mysql_numrows() error in MySQL Query Web Page Display

隐身守侯 提交于 2019-12-12 00:12:04

问题


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

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?

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

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


回答1:


try this

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



回答2:


Use This:

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

The error is caused because you have not closed the localhost in quotes i.e. 'localhost' ...

Hope this helps.




回答3:


use this

mysql_connect("localhost",$username,$password);

and you have syntax error in your query , 1stly run that query in mysql , then use it in the code. Thanks



来源:https://stackoverflow.com/questions/10477159/mysql-numrows-error-in-mysql-query-web-page-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!