I'm trying to get a list of values to display upon a selection from a dropdown menu, but I think my SQL is not quite correct. This is a modification of a W3Schools tutorial http://www.w3schools.com/php/php_ajax_database.asp
I get the error: "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/virtual/hafdal.dk/public_html/test/getuser.php on line 28" when I try to do a selection.
I can't seem to find out what the error is, this function should be able to return multiple rows as I want it to. It might be because it's almost 2 am or just cause I'm dense ;-)
See an example here: hafdal.dk/public_html/test/getuser.php
Here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php header('Content-Type:text/html; charset=UTF-8');
$q=$_GET["q"];
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server) or die("Unable to select database: " . mysql_error());
$sql="SELECT * FROM view_bæjartal WHERE hrepparid = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Bær/Hús/Þurrabúð</th>
<th>Sýsla</th>
<th>Lat</th>
<th>Long</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo utf8_encode("<td>" . $row['bæir'] . "</td>");
echo utf8_encode("<td>" . $row['slysla'] . "</td>");
echo utf8_encode("<td>" . $row['hreppur'] . "</td>");
echo utf8_encode("<td>" . $row['lat'] . "</td>");
echo utf8_encode("<td>" . $row['long'] . "</td>");
echo "</tr>";
}
echo "</table>";
mysql_close($db_server);
?>
<body>
</body>
</html>
I hope someone can help :-)
$result = mysql_query($sql) or die(mysql_error());
– haynar Dec 11 '11 at 0:55$sql="SELECT * FROM view_bæjartal WHERE hrepparid = '".mysql_real_escape_string($q)."'";
– PeeHaa Dec 11 '11 at 0:57