I am trying to query by means of stored procedure.
Stored procedure "name_employee" is working.
I am not sure how to convert the variable "$sql" into number,
person.html
- Code: Select all
<html>
<head>
<title>
Ajax demo 2 using PHP and MySQL by Clement Yap
</title>
<script type="text/javascript">
function showUser(str)
{
if (str=="")//If nothing, then do nothing.
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for new browsers IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for old browsers 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="people" onchange="showUser(this.value)">
<option value="0">Select a person:</option>
<option value="1">Clement</option>
<option value="2">Ellen</option>
<option value="3">Joseph</option>
<option value="4">Louis</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
getuser.php
- Code: Select all
<?php
$q=$_GET['q'];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("employee", $con);
//$sql="SELECT * FROM people WHERE id = '".$q."'";//by direct mysql syntax
$sql="call name_employee("intval($q)")";//by stored procedure
$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);
?>
Please advise
Thanks
Clement


