0

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 :-)

3
  • 1
    are you sure that the collation of your db and table is utf-8? because as I see you're using unicode symbols in the name of table. anyway it's a good idea to print out the error if there any. $result = mysql_query($sql) or die(mysql_error()); Commented Dec 11, 2011 at 0:55
  • 1
    Your first error is using w3schools. Commented Dec 11, 2011 at 0:56
  • 1
    PS: escape the input before doing a query. $sql="SELECT * FROM view_bæjartal WHERE hrepparid = '".mysql_real_escape_string($q)."'"; Commented Dec 11, 2011 at 0:57

2 Answers 2

1

The reason might that that w3schools tutorial, as usual, forgot the proper escaping function. This might lead to a syntax error, and due to the absence of any error checking code a failure with the loop.

Right after your mysql_query() add a mysql_error() call:

$result = mysql_query($sql)  or  print(mysql_error());

It could also just be your Unicode table name. (Then add backticks.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for helping! I went back to the original query and didn't get the same error again. So I'm thinking it is an sql query error. And now I'm not sure if I should post a new question or continue this tread...?
Look again what PeeHaa said. Might explain your issue.
1

You can display errors by calling echo mysql_error($db_server); right after your queried the database (with mysql_query($sql)).

Maybe there is a problem with your table/view name, but to know this for sure you need to have a look at the actual error message returned by mysql.

1 Comment

I do believe you are right with regards to there being a problem with my view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.