Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select警告:mysql_fetch_array()預計參數1是資源,空給出。
我想學習如何正確使用AJAX與PHP和MySQL。我已經看到了這個網站對於我的工作同樣的例子其他人的職位,但我還沒有看到我有同樣的問題任何事情。
該網站保持的源代碼是PHP - AJAX and PHP。
我有我的託管代碼的網站是here。
第一個環節都有工作例和第二鏈接顯示你我試圖找出錯誤。
我試過使用mysqli,但它說了一些關於期待兩個參數的內容,所以我將它改回來了。如果任何人有關於如何使其起作用的建議,我將不勝感激。
錯誤:mysql_fetch_array()預計參數1是資源,在/data/multiserv/users/748953/projects/1801445/www/test/getuser.php空給出上線24
的HTML代碼:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Ajax-PHP/MySQL Cooperation Test</title>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint">Person info will be listed here.</div>
</body>
</html>
PHP:
<?php
$q=$_GET["q"];
include("dbinfo.inc.php");
$con = mysql_connect(localhost, $username, $password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $con);
$resul1 = mysql_query('SELECT * FROM `ajax_demo` WHERE id = \'\".$q.\"\'') or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
難道是錯字嗎? $ resul1 = ...然後fetch_array($ result) – Sverker84 2012-01-10 07:35:47
請在開發中使用'error_reporting(E_ALL)',你會發現錯字和undefined變量。 – 2012-01-10 07:37:49
我正在試驗爲什麼結果沒有出現,我發現這是一個建議的解決方案。這是一個錯字,因爲我沒有改變一切,但結果仍然沒有顯示,我不知道爲什麼。感謝您指出了這一點。 – 2012-01-10 14:14:06