mysql_fetch_array():错误消息

时间:2011-12-11 00:48:08

标签: php mysql

我正在尝试从下拉菜单中获取要显示的值列表,但我认为我的SQL不太正确。这是对W3Schools教程http://www.w3schools.com/php/php_ajax_database.asp

的修改

我收到错误:“警告:mysql_fetch_array():当我尝试做时,提供的参数不是第28行/home/virtual/hafdal.dk/public_html/test/getuser.php中的有效MySQL结果资源”选择。

我似乎无法找出错误是什么,这个函数应该能够返回我想要的多行。可能是因为它差不多凌晨2点,或者只是因为我很密集; - )

请在此处查看示例:hafdal.dk/public_html/test/getuser.php

这是我的代码:

<!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>

我希望有人可以提供帮助: - )

2 个答案:

答案 0 :(得分:1)

您可以在查询数据库后立即调用echo mysql_error($db_server);来显示错误(使用mysql_query($sql))。

您的表/视图名称可能存在问题,但要确定您需要查看mysql返回的实际错误消息。

答案 1 :(得分:1)

原因可能是w3schools教程像往常一样忘记了正确的转义功能。这可能会导致语法错误,并且由于没有任何错误检查代码,因此循环失败。

mysql_query()添加mysql_error()电话之后:

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

它也可能只是您的Unicode表名。 (然后添加反引号。)

相关问题