0

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!

2
  • the correct function is mysql_num_rows Commented May 7, 2012 at 5:40
  • I changed it, but it still throws the error. Commented May 7, 2012 at 5:48

3 Answers 3

2

try this

mysql_connect('localhost',$username,$password);
$num=mysql_num_rows($result);
Sign up to request clarification or add additional context in comments.

3 Comments

That fixed the localhost error, but not the mysql_numrows error. Thanks!
Ross please echo your query and run in your database to check if there is any error in your query and please mind that function is mysql_num_rows() not mysql_numrows() ok. Thanks
Fixed - phpMyAdmin threw in some extra \s that made PHP freak out. Thanks!
0

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.

1 Comment

Yep. Already changed that, but that didn't fix the mysql error. Thanks!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.