尝试从mysql数据库显示表时出错

时间:2015-07-03 05:33:16

标签: php mysql database

我收到此错误:

  

警告:mysql_fetch_assoc()要求参数1为资源,第53行的C:\ xampp \ htdocs \ keytracker \ keytracker.php中给出布尔值   ID名称公司Out_Date Due_Date

整个源代码是:

<html>
<head>
<title>SRE | Key Tracker</title>
<link rel = "stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="searchandregister">
<h2 class="cat-title">Search Booked Keys</h2>
<form method="POST" action="">
<input class="field" type="text" placeholder="Key ID" onfocus="this.placeholder=''" onblur="this.placeholder='Key ID'">
<input class="field" type="text" placeholder="Property Address" onfocus="this.placeholder=''" onblur="this.placeholder='Property Address'">
<input class="field" type="text" placeholder="Name" onfocus="this.placeholder=''" onblur="this.placeholder='Name'">
<input class="field" type="text" placeholder="Creditor" onfocus="this.placeholder=''" onblur="this.placeholder='Creditor'">
<br/><input class="button" type="submit" value="Search">
</form><br/>


<h2 class="cat-title">Book Out New Key</h2>
<form method="POST" action="">
<input class="field" type="text" placeholder="Key ID" onfocus="this.placeholder=''" onblur="this.placeholder='Key ID'">
<input class="field" type="text" placeholder="Property Address" onfocus="this.placeholder=''" onblur="this.placeholder='Property Address'">
<input class="field" type="text" placeholder="Name" onfocus="this.placeholder=''" onblur="this.placeholder='Name'">
<input class="field" type="text" placeholder="Creditor" onfocus="this.placeholder=''" onblur="this.placeholder='Creditor'">
<br/><input class="button" type="submit" value="Register">
</form>

</div>

<div class="results">
<h2 class="cat-title-right">Results</h2>
<?php
    //Make connection
        mysql_connect('localhost', 'access', 'AR51Bigwater');
    //Select DB
        mysql_select_db('keytracker');

    $sql="SELECT * FROM keys";
    $records = mysql_query($sql);
    //Display Data

?>
<table cellpadding="1" cellspacing="1">
<tr>

<th>ID</th>
<th>Name</th>
<th>Company</th>
<th>Out_Date</th>
<th>Due_Date</th>
<tr>

<?php
while ($keys = mysql_fetch_assoc($records)){
    echo "<tr>";
    echo "<td>".$keys['ID']."</td>";
    echo "<td>".$keys['Name']."</td>";
    echo "<td>".$keys['Company']."</td>";
    echo "<td>".$keys['Out_Date']."</td>";
    echo "<td>".$keys['Due_Date']."</td>";
    echo "</tr>";
}   
?>
</table>
</div>

错误所在的区域是:

<?php
while ($keys = mysql_fetch_assoc($records)){
    echo "<tr>";
    echo "<td>".$keys['ID']."</td>";
    echo "<td>".$keys['Name']."</td>";
    echo "<td>".$keys['Company']."</td>";
    echo "<td>".$keys['Out_Date']."</td>";
    echo "<td>".$keys['Due_Date']."</td>";
    echo "</tr>";
}   
?>

任何帮助都将不胜感激。

4 个答案:

答案 0 :(得分:1)

**Check Whether you have any record in database**

   <?php
   if(mysql_num_rows($records) > 0)
     while ($keys = mysql_fetch_assoc($records)){
    echo "<tr>";
    echo "<td>".$keys['ID']."</td>";
    echo "<td>".$keys['Name']."</td>";
    echo "<td>".$keys['Company']."</td>";
    echo "<td>".$keys['Out_Date']."</td>";
    echo "<td>".$keys['Due_Date']."</td>";
    echo "</tr>";
    }
}else{
 echo "No Recored" ;
}

答案 1 :(得分:0)

您必须将mysql_connect分配给变量并使用它。

<?php
    //Make connection
        $conn = mysql_connect('host', 'user', 'pass', 'db');

    $sql="SELECT * FROM keys";
    $records = mysql_query($sql, $conn);
    $rows = mysql_num_rows($result);
    //Display Data

?>
<table cellpadding="1" cellspacing="1">
<tr>

<th>ID</th>
<th>Name</th>
<th>Company</th>
<th>Out_Date</th>
<th>Due_Date</th>
<tr>

<?php
if ($rows > 0) {
    while ($keys = mysql_fetch_assoc($records)){
        echo "<tr>";
        echo "<td>".$keys['ID']."</td>";
        echo "<td>".$keys['Name']."</td>";
        echo "<td>".$keys['Company']."</td>";
        echo "<td>".$keys['Out_Date']."</td>";
        echo "<td>".$keys['Due_Date']."</td>";
        echo "</tr>";
    }   
}
?>
</table>

另请注意:

  

警告
  自PHP 5.5.0起,此扩展已弃用,将来将被删除。相反,应该使用MySQLi或PDO_MySQL扩展。另请参阅MySQL:选择API指南和相关的常见问题解答以获取更多信息。该功能的替代方案包括:
     mysqli_query()
     PDO ::查询()

答案 2 :(得分:0)

您的结果数组$ records可能为null。 尝试使用以下代码

$num=mysql_num_rows($records);
if($num){
 while ($keys = mysql_fetch_assoc($records)){
        echo "<tr>";
        echo "<td>".$keys['ID']."</td>";
        echo "<td>".$keys['Name']."</td>";
        echo "<td>".$keys['Company']."</td>";
        echo "<td>".$keys['Out_Date']."</td>";
        echo "<td>".$keys['Due_Date']."</td>";
        echo "</tr>";
    } 
}
else {
 $msg = "No records found";
}

答案 3 :(得分:0)

你检查过,mysql记录是否返回?您的查询很可能失败,因此mysql_query();返回false。

请覆盖您的while()声明代码:

if($records){//Check for query return
$num_rows = mysql_num_rows($records);
if(0==$num_rows)
{//Check for records avail
             echo "No record";
}
else
{
           while ($keys = mysql_fetch_assoc($records)){
             echo "<tr>";
             echo "<td>".$keys['ID']."</td>";
             echo "<td>".$keys['Name']."</td>";
             echo "<td>".$keys['Company']."</td>";
             echo "<td>".$keys['Out_Date']."</td>";
             echo "<td>".$keys['Due_Date']."</td>";
             echo "</tr>";
           } 
}
}
else 
{
print("<pre> ERROR :: ");
print_r(error_get_last());
print("</pre>");
}

检查它打印的内容。如果这将打印ERROR,请检查它是什么。

注意:请注意,自PHP 5.5.0起,不推荐使用mysql函数,将来会删除它们。如需更多检查here

PS:如果这些东西可以帮到你,请给我留言和支持:)