1

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

I'm trying to learn how to use AJAX correctly with PHP and MySQL. I've seen other people's posts on this site regarding the same example I'm working with, but I haven't seen anything about the same issues I'm having.

The site holding the source code is PHP - AJAX and PHP.
The site I have hosting my code is here.

The first link has a working example and the second link shows you the error I'm trying to figure out.

I tried to use mysqli, but it said something about expecting two parameters, so I changed it back. If anyone has suggestions on how to get it to work, I'd greatly appreciate it.

Error: mysql_fetch_array() expects parameter 1 to be resource, null given in /data/multiserv/users/748953/projects/1801445/www/test/getuser.php on line 24

The html code:

<!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);
?> 
Community
  • 1
  • 1
pv2.al1290
  • 95
  • 1
  • 1
  • 9
  • 4
    Could it be a typo? $resul1 = ... and then fetch_array($result) – Sverker84 Jan 10 '12 at 07:35
  • 4
    Please use `error_reporting(E_ALL)` in development, you would have caught the typo and undefined variable. – Wesley Murch Jan 10 '12 at 07:37
  • I was experimenting with why the results weren't showing up and I found that as a proposed solution. It was a typo since I didn't change everything, but the results still don't show up and I'm not sure why. Thanks for pointing that out. – pv2.al1290 Jan 10 '12 at 14:14
  • Finally I was able to fix the issue of it not showing up. Thank god for this site! – pv2.al1290 Jan 10 '12 at 15:42

5 Answers5

2

You have a spelling mistake you are assigning value in $resul1 and using mysql_fetch_array($result) notice $result

shankhan
  • 6,343
  • 2
  • 19
  • 22
1

first try to check the data of $q

and after that try with this query:

mysql_query("SELECT * FROM `ajax_demo` 
             WHERE id = '" . mysql_real_escape_string($q) ."'");
jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
  • The issue for the error code was a typo, but your second code completely fixed my issue! I couldn't figure out why the data wouldn't show, but for whatever reason, I guess the coding for my MySQL version needed mysql_real_escape_string($q) isntead of .$q . So thank you! – pv2.al1290 Jan 10 '12 at 15:41
0

Change $resul1 to $result1, you did wrote wrong there.

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
vvr
  • 456
  • 3
  • 17
  • 30
0

change your while statement into this:

while($row = mysql_fetch_array($result1))

Edit:

Make sure to always review your code and always use a consistent naming convention, the error indicated by the server is already enough to detect to which part of the program the issue is occuring (no need to run any other function if you want to have a complete debugging settings for you development server set it up on the configuration files instead of doing it on the application itself but turn it off for the live server for security purposes).

"Error: mysql_fetch_array() expects parameter 1 to be resource, null given in /data/multiserv/users/748953/projects/1801445/www/test/getuser.php on line 24"

which means that the error is happening on line 24 of the file "/data/multiserv/users/748953/projects/1801445/www/test/getuser.php" and that the variable on the first parameter you have used on the function "mysql_fetch_array" is the one causing the problem because of the statement "expects parameter 1 to be resource" so you should go directly to which part of the program this error is pointing which is helpful for any programmer. ;)

Christopher Pelayo
  • 792
  • 11
  • 30
  • Thanks for the insight. I seem to have an inability to understand exactly what the error messages mean. Do you think there's a way to have it report both the error message and number? – pv2.al1290 Jan 10 '12 at 14:16
  • no problem. for your question the error message message is already returned by the server the one that states "mysql_fetch_array() expects parameter 1 to be resource", the next part of the error is pointing to which file it occurs like "null given in /data/multiserv/users/748953/projects/1801445/www/test/getuser.php" and the next statement will state which line number it occurs like "on line 24" with this details it would be more easier for you to track which part of the program is causing the issue and you could review the code as necessary. – Christopher Pelayo Jan 10 '12 at 22:57
0

You have a spelling mistake you are assigning value in $resul1 and using mysql_fetch_array($result) notice $result.

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
Jack
  • 1