2010-03-28 54 views
3

我正在使用mysqli_stmt_bind_param()創建一個INSERT語句。出於某種原因,我收到錯誤。我使用mysqli_error()來查看錯誤消息,但它不是特別有用。返回由mysqli_stmt_bind_param創建的語句

有沒有辦法看到什麼查詢實際上正在執行?

所產生的誤差:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc,date,expdate,mintix,maxtix,contactname,contactemail,contactphone) VALUES (?' at line 1

+3

請告訴我們你的代碼。否則很難幫助你。 – 2010-03-28 00:13:00

+0

除了byronh評論之外,您還應該能夠將查詢語句存儲在變量中,然後{query,echo,save}查詢語句和錯誤代碼。 – bdl 2010-03-28 00:14:33

+1

@bdl我無法找到一種方法將準備好的語句轉換回字符串(我認爲bigmac所要求的是什麼)。 – 2010-03-28 00:15:45

回答

2

由mysqli_prepare()創建的準備語句是服務器端準備好的語句。
當你執行這樣一個準備好的語句時,只傳輸語句ID和參數,而不是某些查詢字符串,就像你用實際參數(客戶端,即你的PHP腳本)替換佔位符一樣。
但是你可以看到的結果在MySQL服務器的通用日誌,看到Prepared Statement Logging

編輯:你的情況,因爲desc是保留關鍵字聲明的準備失敗。
有關關鍵字的列表以及如何使用它們作爲標識符(如果需要的話)看到http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

$q = ' 
    INSERT INTO 
    `event` 
    (
     `cityid`, `name`, `desc`, `date`, 
     `expdate`, `mintix`, `maxtix`, 
     `contactname`, `contactemail`, `contactphone` 
    ) 
    VALUES 
    (
     ?,?,?,?, 
     ?,?,?, 
     ?,?,? 
    ) 
'; 

if (false===($stmt=mysqli_prepare($dblink, $q))) { 
    /* 
    in production-code you might not want to reveal 
    the error string to each and every user 
    ...but for this example and for debugging purposes: 
    */ 
    die('mysqli_prepare failed: '.htmlspecialchars(mysqli_error($dblink))); 
} 

$rc = mysqli_stmt_bind_param(
    $stmt, 
    "issssiisss", 
    $city,$name,$desc,$date, 
    $expdate,$mintix,$maxtix, 
    $contactname,$contactemail,$contactphone 
); 
if (false===$rc) { 
    die('mysqli_stmt_bind_param failed: '.htmlspecialchars(mysqli_stmt_error($stmt))); 
} 


if (false===mysqli_stmt_execute($stmt)) { 
    die('mysqli_stmt_execute failed: '.htmlspecialchars(mysqli_stmt_error($stmt))); 
} 

mysqli_stmt_close($stmt); 
+0

啊,所以*這就是爲什麼mysql和PDO都不能正確支持這一點。謝謝。 – 2010-03-28 17:06:22

+0

PDO可以同時執行服務器端和客戶端(模擬)預準備語句(PDO :: ATTR_EMULATE_PREPARES)。在後一種情況下,儘管它看起來沒有被公開,但它可能會打印查詢字符串。 – VolkerK 2010-03-28 17:47:36

+0

我以爲我以前用過desc作爲列名,但我錯了。我很驚訝phpMyAdmin讓我使用它。我只是試圖改變它,phpMyAdmin顯示一個錯誤。它仍然設法改變它,現在一切正常。 感謝所有這些方法來捕捉錯誤。這真的很有用。沒有意識到所有這些都是可能的。 – burger 2010-03-28 18:03:50