MySQL查询网页显示中的mysql_numrows()错误

时间:2012-05-07 05:37:21

标签: 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 AM - 我在代码中添加了$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 AM - 我修好了。 PHPMyAdmin添加了我不需要的额外反向。 谢谢!

3 个答案:

答案 0 :(得分:2)

试试这个

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

答案 1 :(得分:0)

使用此:

$username = 'root';
$password = 'root';
mysql_connect('localhost',$username,$password);

导致错误是因为您尚未关闭引号中的localhost,即'localhost' ...

希望这有帮助。

答案 2 :(得分:0)

使用此

的mysql_connect( “本地主机”,$用户名,密码$);

你的查询中有语法错误,首先在mysql中运行该查询,然后在代码中使用它。感谢