【问题标题】:php SQL statements don't work, but when I try to use statements in a DBMS, they do workphp SQL 语句不起作用,但是当我尝试在 DBMS 中使用语句时,它们确实起作用
【发布时间】: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);

}

【问题讨论】:

标签: php mysql error-handling insert


【解决方案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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2014-06-19
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多