2013-03-08 6 views
0

以下代碼執行錯誤查詢。輸入數據顯然是錯誤的。即使有這些錯誤,MySQLi也不會報告錯誤。PHP和MySQLi - 錯誤查詢並且沒有錯誤

<?php 
$mysqli = new MySQLi('hostname', 'username', 'password', 'databasename'); 
$sql = ' 
SELECT 
    ID_ASSOCIADO 
FROM 
    MOVIMENTO 
WHERE 
    DATA_MOVIMENTO BETWEEN ? AND ? 
'; 
$mysqliStmt = $mysqli->prepare($sql); 
$dataInicial = "2013-02-0'"; // Malformed date. 
$dataFinal = '2013-02-"'; // Another one. 
$mysqliStmt->bind_param('ss', $dataInicial, $dataFinal); 
$mysqliStmt->execute(); 
$mysqliStmt->bind_result($idAssociado); 
while ($mysqliStmt->fetch()) { 
    echo 'ID ASSOCIADO: ' . $idAssociado . PHP_EOL; 
} 
?> 

的準備調用後的mysqli對象:

mysqli Object 
(
    [affected_rows] => -1 
    [client_info] => 5.5.29 
    [client_version] => 50529 
    [connect_errno] => 0 
    [connect_error] => 
    [errno] => 0 
    [error] => 
    [error_list] => Array 
     (
     ) 

    [field_count] => 0 
    [host_info] => Localhost via UNIX socket 
    [info] => 
    [insert_id] => 0 
    [server_info] => 5.5.28-1 
    [server_version] => 50528 
    [stat] => Uptime: 9651 Threads: 1 Questions: 1947 Slow queries: 0 Opens: 1433 Flush tables: 1 Open tables: 340 Queries per second avg: 0.201 
    [sqlstate] => 00000 
    [protocol_version] => 10 
    [thread_id] => 481 
    [warning_count] => 0 
) 

mysqli的對象執行調用後:

mysqli Object 
(
    [affected_rows] => -1 
    [client_info] => 5.5.29 
    [client_version] => 50529 
    [connect_errno] => 0 
    [connect_error] => 
    [errno] => 0 
    [error] => 
    [error_list] => Array 
     (
     ) 

    [field_count] => 1 
    [host_info] => Localhost via UNIX socket 
    [info] => 
    [insert_id] => 0 
    [server_info] => 5.5.28-1 
    [server_version] => 50528 
    [stat] => Commands out of sync; you can't run this command now 
    [sqlstate] => HY000 
    [protocol_version] => 10 
    [thread_id] => 481 
    [warning_count] => 0 
) 

我必須把錯誤在庫MySQLi像PDO?

+0

爲什麼要這個報告錯誤?你查詢的數據,你沒有得到任何。就是這樣,你的查詢仍然有效。 – Oliver 2013-03-08 16:19:57

回答

1

事情是2013-02-0'2013-02-"是有效的MySQL日期字符串。 2013-02-0'評估爲2012-02-002013-02-"評估爲NULL,但仍然可以與NULL比較,因此該查詢實際上不是無效的。

mysql> SELECT DATE("2012-02-0'"); 
+--------------------+ 
| DATE("2012-02-0'") | 
+--------------------+ 
| 2012-02-00   | 
+--------------------+ 
1 row in set (0.00 sec) 

mysql> SELECT DATE('2012-02-"'); 
+-------------------+ 
| DATE('2012-02-"') | 
+-------------------+ 
| NULL    | 
+-------------------+ 
1 row in set, 1 warning (0.03 sec) 


mysql> SELECT '2012-02-00' BETWEEN "2013-02-'" AND '2013-02-0"'; 
+---------------------------------------------------+ 
| '2012-02-00' BETWEEN "2013-02-'" AND '2013-02-0"' | 
+---------------------------------------------------+ 
|             0 | 
+---------------------------------------------------+ 
1 row in set (0.00 sec) 
+0

我現在沒有例子,但是我得到的結果格式不正確...我會嘗試再次重現奇怪行爲,然後更新我的問題。謝謝。 – 2013-03-08 16:26:18

1

你所擁有的mysqli檢查錯誤的方式與舊版本的MySQL

$mysqliStmt = $mysqli->prepare($sql) or trigger_error($mysqli->error); 
$mysqliStmt->bind_param('ss', $dataInicial, $dataFinal) or trigger_error($mysqli->error); 
$mysqliStmt->execute() or trigger_error($mysqli->error); 

等相同。
所以,只是退出不可用的mysqli和使用PDO