mysql_numrows()在MySQL查询网页显示错误 [英] mysql_numrows() error in MySQL Query Web Page Display

查看:366
本文介绍了mysql_numrows()在MySQL查询网页显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个网页,选择从符合条件的数据库的某些字段。到数据库建立连接,但没有显示表,但表头。在Apache2的日志我看

I am writing a web page that selects certain fields from a database that meet a criteria. A connection to the database is made, but nothing displays in the table but the header. In the Apache2 logs I see

[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

下面是code我使用的是:

Here is the code I'm using:

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

什么我需要做修复它?

What do I need to do to fix it?

- 更新上午1点51分 - 我加了 $ ERROR_MSG = mysql_error();?&LT; PHP的echo $ ERROR_MSG&GT; 我的code和现在我得到你在你的SQL语法错误;检查对应于你的MySQL服务器版本使用附近的正确语法手册'\\'\\'和medicalAlert IS NOT NULL ORDER BY姓氏,品位'在行1
我需要什么,我查询的改变?

--Update 1:51 AM - I added $error_msg = mysql_error(); and <?php echo $error_msg ?> to my code and now I get 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 What do I need to change in my query?

- 更新上午01时54分 - 我固定它。 PHPMyAdmin中添加了额外的反斜杠,我并不需要。
谢谢!

--Update 1:54 AM - I fixed it. PHPMyAdmin added extra backslashed that I didn't need. Thanks!

推荐答案

试试这个

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

这篇关于mysql_numrows()在MySQL查询网页显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆