MySQL錯誤我怎樣才能得到的錯誤從下面我的MySQL聲明顯示或是否有更好的方式來寫下面的與PDO或相似:充分利用聲明
代碼:
$mysqlFiles = "SELECT campaign_image, optout_image FROM campaigns WHERE id = '$campaign_id'";
while($data = mysql_fetch_assoc($mysqlFiles));
{
$imageNames[] = $data;
}
MySQL錯誤我怎樣才能得到的錯誤從下面我的MySQL聲明顯示或是否有更好的方式來寫下面的與PDO或相似:充分利用聲明
代碼:
$mysqlFiles = "SELECT campaign_image, optout_image FROM campaigns WHERE id = '$campaign_id'";
while($data = mysql_fetch_assoc($mysqlFiles));
{
$imageNames[] = $data;
}
你的代碼有一點點不正確。
mysql_fetch_assoc
,而不是資源。mysql_*
函數已棄用。您應該使用mysqli_*
函數。一旦您糾正了這些問題,您可以使用mysqli_error
查看錯誤消息。
你得到了我的支持。你接受這個作爲另一個答案嗎? – RiggsFolly
使用此(來源:http://php.net/manual/en/function.mysql-error.php):
mysql_error ($mysqlconnection);
首先...在這行代碼:
while($data = mysql_fetch_assoc($mysqlFiles));
^end of statement, stops the process.
不管你信不信,被認爲是有效的語法(而不會拋出,因爲它的誤差),但 ...也結束你的發言,將不會處理{ $imageNames[] = $data; }
。
查詢是否成功,它仍然會在那裏停止。
刪除它
while($data = mysql_fetch_assoc($mysqlFiles))
參考上分號:
「如同在C或Perl,PHP要求指令是終止用ea結尾的分號ch聲明。一段PHP代碼的結束標記自動包含分號;你不需要用分號來終止PHP塊的最後一行。塊的結束標記將包括緊接着的換行符(如果存在的話)。「
如果你想捕捉到潛在的錯誤,使用mysql_error()
針對您的查詢
別的東西,我注意到的是,你不」查詢「與mysql_query()
。
所以你的代碼將永遠不會導致與什麼已經說過沿着成功的查詢。從手動
實施例:
$query = sprintf("SELECT firstname, lastname, address, age FROM friends
WHERE firstname='%s' AND lastname='%s'",
mysql_real_escape_string($firstname),
mysql_real_escape_string($lastname));
// Perform Query
$result = mysql_query($query);
實施例從http://php.net/manual/en/function.mysql-fetch-assoc.php
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
如果下面分別編碼爲這樣並且在用分號,就不會處理內部{...}
任何東西:
while ($row = mysql_fetch_assoc($result)); {
// ^semi-colon terminator similar to yours
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
我希望這會爲某人服務。我想我解釋得很好,除非我沒有把握這個問題。 –
http://php.net/manual/en/function.mysql-error.php?我不太在意這裏。你是否故意造成錯誤? –
您需要在提取數據之前執行查詢。查看['mysql_query()'](http://php.net/manual/en/function.mysql-query.php)的例子。另外,是的,我強烈建議使用MySQLi或PDO。 – showdev
你究竟想要什麼。我不是很清楚。錯誤可能寫入您的php.ini定義的地方 – Rik