2012-01-10 117 views
1

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); 
?> 
+4

难道是错字吗? $ resul1 = ...然后fetch_array($ result) – Sverker84 2012-01-10 07:35:47

+4

请在开发中使用'error_reporting(E_ALL)',你会发现错字和undefined变量。 – 2012-01-10 07:37:49

+0

我正在试验为什么结果没有出现,我发现这是一个建议的解决方案。这是一个错字,因为我没有改变一切,但结果仍然没有显示,我不知道为什么。感谢您指出了这一点。 – 2012-01-10 14:14:06

回答

1

第一次尝试检查$q

的数据,之后尝试与此查询:

mysql_query("SELECT * FROM `ajax_demo` 
      WHERE id = '" . mysql_real_escape_string($q) ."'"); 
+0

错误代码的问题是一个错字,但你的第二个代码完全解决了我的问题!我无法弄清楚为什么数据不会显示,但无论出于何种原因,我猜我的MySQL版本的编码需要mysql_real_escape_string($ q)而不是。$ q。所以谢谢! – 2012-01-10 15:41:34

2

你有拼写错误,你在$resul1分配值,并使用mysql_fetch_array($结果)通知$result

+0

这是一个错字。感谢您指出了这一点。 – 2012-01-10 14:14:21

0

变化$resul1$result1,你没写错那里。

+0

除了它应该只有$结果,如果你看其他代码... – Sverker84 2012-01-10 07:39:37

+0

这是一个错字。感谢您指出了这一点。 – 2012-01-10 14:14:46

0

改变你的while语句变成这样:

while($row = mysql_fetch_array($result1)) 

编辑:

确保始终检查您的代码并始终使用一致的命名约定,服务器指出的错误已经足以检测问题发生在程序的哪一部分(如果你想为你的开发服务器设置一个完整的调试设置,则不需要运行任何其他功能)文件,而不是在应用程序本身上执行它,但出于安全目的将其关闭用于实时服务器)。

“错误:mysql_fetch_array()预计参数1是资源,在/data/multiserv/users/748953/projects/1801445/www/test/getuser.php空给定在线路24”

这意味着错误发生在文件“/data/multiserv/users/748953/projects/1801445/www/test/getuser.php”的第24行上,并且您在函数“mysql_fetch_array”上使用的第一个参数上的变量是由于语句“期望参数1是资源”而导致问题的原因,因此您应该直接转到该错误指向的程序的哪一部分,这对任何程序员都有帮助。;)

+0

感谢您的洞察力。我似乎无法准确理解错误消息的含义。你认为有办法让它报告错误信息和数字吗? – 2012-01-10 14:16:25

+0

没问题。对于你的问题,错误消息消息已经被服务器返回,表明“mysql_fetch_array()期望参数1是资源”,错误的下一部分指向哪个文件出现,如“/ data/multiserv/users/748953/projects/1801445/www/test/getuser.php“,下一条语句将声明它出现在哪个行号,如”在线24“,这个详细信息会更容易跟踪哪部分该程序导致该问题,您可以根据需要查看代码。 – 2012-01-10 22:57:58

0

你有一个拼写错误,你在$resul1中分配值,并使用mysql_fetch_array($result)通知$result

+0

谢谢。这是错误的问题。 – 2012-01-10 15:31:09

相关问题