2014-09-20 105 views
0

以下是我正在處理的php代碼,插入查詢似乎在我將其放入phpMyAdmin時起作用(使用XAMPP),但$狀態由於某種原因在此處運行查詢時返回false ...我嘗試了更改Insert語句和執行文件輸出檢查以查看問題來自何處的每種組合,但是我完全被難住現在。有任何想法嗎?php SQL語句不起作用,但是當我嘗試在DBMS中使用語句時,它們可以工作

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

使用['mysql_error'(http://php.net/mysql_error),以獲得用於調試的原因。 – Gumbo 2014-09-20 06:26:10

回答

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();