2011-11-30 51 views
-1

我不斷收到相同的mysql錯誤代碼,但我不知道如何更正它。在調用mysql_fetch_array()時得到「提供的參數不是有效的MySQL結果資源」

錯誤:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /websites/123reg/LinuxPackage22/we/ez/y_/weezy.co.uk/public_html/search.php on line 29 

我已經標示出下面的代碼行29。

這是什麼意思?

感謝

<?php 
// Change the fields below as per the requirements 
$db_host=""; 
$db_username=""; 
$db_password=""; 
$db_name=""; 
$db_tb_name="data"; 
$db_tb_atr_name="name"; 
$db_tb_atr_name="email"; 
$db_tb_atr_name="location"; 


//Now we are going to write a script that will do search task 
// leave the below fields as it is except while loop, which will display results on screen 

mysql_connect("$db_host","$db_username","$db_password"); 
mysql_select_db("$db_name"); 

$query=mysql_real_escape_string($_GET['query']); 

$query_for_result=mysql_query(" 

SELECT name, email, location 

FROM data WHERE 

$db_tb_atr_name like '%".$query."%'"); 
echo "<h2>Search Results</h2><ol>"; 
while ($row = mysql_fetch_array($result)) <<<<<<<<<<<<<<< LINE 29 
{ 
echo "<li>"; 
echo substr($row["name"], $row["email"], $row["location"]); 
echo "</li><hr/>"; 
} 
echo "</ol>"; 

mysql_close(); 
?> 
+0

檢查,如果要求全成:'如果($ query_for_result){''} ' – noob

+1

[Warning:mysql_fetch_array():提供的參數可能重複不是有效的MySQL結果](http://stackoverflow.com/questions/795746/warning-mysql-fetch-array-supplied-argument-is-not- a-valid-mysql-result) – Fluffeh

回答

6
while ($row = mysql_fetch_array($result)) 

你在哪裏設置$result什麼嗎?

你可能是指

while ($row = mysql_fetch_array($query_for_result)) 
1

你存儲在名爲$query_for_result變量您查詢的結果,後來就嘗試讀取$result,裏面是空的。

1

你分配的mysql_query$query_for_result的結果,但隨後在mysql_fetch_array()試圖遍歷$result。試試這個:

$result = mysql_query("..."); 
while ($row = mysql_fetch_array($result)) { 
    ... 

OR

$query_for_result = mysql_query("..."); 
while ($row = mysql_fetch_array($query_for_result)) { 
    ... 
+0

謝謝,我這樣做了,但現在沒有顯示,不是錯誤消息或搜索結果,接下來我該做什麼? –

0

您也應該檢查是否有任何返回的行

$rs = mysql_query(" 
    SELECT name, email, location 
    FROM data 
    WHERE ".$db_tb_atr_name." like '%".$query."%' 
"); 

if(mysql_num_rows($rs)){ 

    while ($row = mysql_fetch_array($rs)){ 
    // code 
    } 
} else { 
// no results 
} 
相關問題