【发布时间】:2023-03-23 04:29:01
【问题描述】:
以下是我正在处理的 php 代码,当我将其放入 phpMyAdmin(使用 XAMPP)时,插入查询似乎可以工作,但是在此处运行查询时,$status 出于某种原因返回 false...我'我尝试了更改 Insert 语句和进行文件输出检查的所有组合,以查看问题出在哪里,但到目前为止我完全被难住了。有什么想法吗?
function addQuestion($question_to_add) {
global $host, $username, $password, $dbName, $user_table, $registered_user_table, $question_table;
global $answer_table, $user_answer, $user_post;
connectToDB($username, $password, $host, $dbName);
//$countQuery = "SELECT COUNT(QID) FROM questions";
//$count = mysql_fetch_array(mysql_query($countQuery))[0];//we fetch an array of counts for each column and return the count of column 0
$addQuestionQuery = "INSERT INTO questions (QID, UID, title, _timestamp, numRating, content, category, location) VALUES (0, 7, 0, 0, 0, 0, 0, 0)";
$status = mysql_query($addQuestionQuery);
if ($status == false) {// if the query failed, for whatever reason, let us know.
file_put_contents("out","false");
return false;
}
file_put_contents("out","true");
return true;
}
有用:
function connectToDB($user, $password, $server_host, $db_name) {
$conn = mysql_connect($server_host, $user. $password);
if (!$conn) {
die("Could not connect: " . mysql_error());
//
}
mysql_select_db($db_name, $conn);
}
【问题讨论】:
-
使用
mysql_error获取调试原因。
标签: php mysql error-handling insert