0

我正在编写一个网页,该网页从满足条件的数据库中选择某些字段。建立了与数据库的连接,但表中只显示标题。在我看到的 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 分更新——我修好了。PHPMyAdmin 添加了我不需要的额外反斜杠。谢谢!

4

3 回答 3

2

试试这个

mysql_connect('localhost',$username,$password);
$num=mysql_num_rows($result);
于 2012-05-07T05:40:25.477 回答
0

用这个:

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

该错误是由于您没有关闭localhost引号引起的,即'localhost'...

希望这可以帮助。

于 2012-05-07T05:45:46.870 回答
0

用这个

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

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

于 2012-05-07T06:29:13.467 回答