php - MySQL查询网页显示mysql_numrows()错误

标签 php mysql apache mysql-num-rows

我正在编写一个网页,该网页从数据库中选择满足条件的某些字段。已建立与数据库的连接,但表中除标题外不显示任何内容。在 Apache2 日志中我看到

[Mon May 07 01:30:21 2012] [error] [client MyIP] PHP Notice:  Use of undefined constant localhost - assumed 'localhost' in /var/www/medical.php on line 3
[Mon May 07 01:30:21 2012] [error] [client MyIP] PHP Warning:  mysql_numrows() expects parameter 1 to be resource, boolean given in /var/www/medical.php on line 7

这是我正在使用的代码:

<?php 
include ("/var/www/medicalalerts-config.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$query = "SELECT * FROM `1213-rep` WHERE medicalAlert <> \'\' and medicalAlert IS NOT NULL ORDER BY lastName, grade";
$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();
?>
<!--Results Table-->
<table border="1" align="center" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif" >Name</font></th>
<th><font face="Arial, Helvetica, sans-serif" >Grade</font></th>
<th><font face="Arial, Helvetica, sans-serif" >Medical Alert</font></th>
<th><font face="Arial, Helvetica, sans-serif" >Parent 1 Name</font></th>
<th><font face="Arial, Helvetica, sans-serif" >Parent 1 Phone</font></th>
<th><font face="Arial, Helvetica, sans-serif" >Parent 2 Name</font></th>
<th><font face="Arial, Helvetica, sans-serif" >Parent 2 Phone</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"firstName");
$f2=mysql_result($result,$i,"lastName");
$f3=mysql_result($result,$i,"grade");
$f4=mysql_result($result,$i,"medicalAlert");
$f6=mysql_result($result,$i,"parent1Name");
$f7=mysql_result($result,$i,"parent1Phone");
$f8=mysql_result($result,$i,"parent2Name");
$f9=mysql_result($result,$i,"parent2Phone");

?>

<tr>
<td><font face="Arial, Helvetica, sans-serif" ><?php echo $f1; echo $f2;?> </font></td>
<td><font face="Arial, Helvetica, sans-serif" ><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif" ><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif" ><?php echo $f5; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif" ><?php echo $f6; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif" ><?php echo $f7; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif" ><?php echo $f8; ?></font></td>
</tr>

<?php
$i++;
}
?>
</table>

我需要做什么来修复它?

--凌晨 1:51 更新 - 我添加了 $error_msg = mysql_error();<?php echo $error_msg ?>到我的代码,现在我得到 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'\' and medicalAlert IS NOT NULL ORDER BY lastName, grade' at line 1 我需要在查询中更改什么?

--凌晨 1:54 更新 - 我修复了它。 PHPMyAdmin 添加了我不需要的额外反斜杠。 谢谢!

最佳答案

试试这个

mysql_connect('localhost',$username,$password);
$num=mysql_num_rows($result);

关于php - MySQL查询网页显示mysql_numrows()错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10477159/

相关文章:

php - 出于安全原因,set_time_limit() 已被禁用

php - 如何阻止访问根文件夹中除 index.php 之外的所有 .php(通过 .htaccess)?

python - 使用python for CGI的问题

php - 如何从 php webservice 获取许多 vars 到 iPhone 应用程序?

java - 使用 HTTP URL 将文件名从 Java 传递到 PHP

php - 直接访问 CDN 域时从 CDN 重定向到主网站?

mysql - 如何提高复杂MySQL select查询的性能?

mysql - 使用相同的整数更新 MySQL 数据库的 4,000 多行

mysql - sqlalchemy.exc.ResourceClosedError : This result object does not return rows. 已经自动关闭

apache - 无法在 solr 6.0 中找到 schema.xml 文件,所以要配置它,我应该添加一个新文件,还是会自动发生?