php SQL语句不起作用,但是当我尝试在DBMS中使用语句时,它们确实有效

时间:2014-09-20 06:24:38

标签: php mysql error-handling insert

以下是我正在处理的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);

}

1 个答案:

答案 0 :(得分:1)

连接上的参数错误:

$conn = mysql_connect($server_host, $user. $password);
                                        ^ . it should be ,

注意:使用更好的版本mysqli或PDO。 Mysql现已弃用,不再维护。

与往常一样,在调试时,请始终打开错误报告:

error_reporting(E_ALL);
ini_set('display_errors', '1');
mysql_error();