Jump to content

PHP with Ajax and MySQL Help!


twilitegxa

Recommended Posts

I have the following page:

 

getuser.php

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con);

$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysql_query($sql);

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

 

But I am getting this error:

 

Warning:  mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\getuser.php on line 25

 

I can't figure out what I am doing wrong. I got it from a tutorial for Ajax and PHP with MySQL. Here are the other two pages:

 

html page:

<html>
<head>
<script type="text/javascript" src="selectuser.js"></script>
</head>
<body>

<form>
Select a User:
<select name="users" onchange="showUser(this.value)">
<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"><b>Person info will be listed here.</b></div>

</body>
</html>

 

selectuser.js:

var xmlhttp;

function showUser(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="getuser.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

I got it from this page:

 

http://www.w3schools.com/PHP/php_ajax_database.asp

 

Can anyone help me with why I am getting this error?Here is the table SQL statement:

 

CREATE TABLE IF NOT EXISTS `ajax_demo` (

  `id` int(11) NOT NULL,

  `FirstName` varchar(150) NOT NULL,

  `LastName` varchar(150) NOT NULL,

  `Age` int(11) NOT NULL,

  `Hometown` varchar(150) NOT NULL,

  `Job` varchar(150) NOT NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

INSERT INTO `ajax_demo` (`id`, `FirstName`, `LastName`, `Age`, `Hometown`, `Job`) VALUES

(1, 'Peter', 'Griffin', 41, 'Quahog', 'Brewery'),

(2, 'Lois', 'Griffin', 40, 'Newport', 'Piano Teacher'),

(3, 'Joseph', 'Swanson', 39, 'Quahog', 'Police Officer'),

(4, 'Glenn', 'Quagmire', 41, 'Quahog', 'Pilot');

Link to comment
https://forums.phpfreaks.com/topic/190284-php-with-ajax-and-mysql-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.