dsrnwngq411594 2013-12-06 13:40
浏览 75
已采纳

php和MySQL检索数据[重复]

I'm having a problem with retrieving data,

I have two files **list_item.php** and **mysql_con_links.php**

My mysql_con_links.php has a table with book titles and view details with a link to view more details.

When I click on view more details i get this error:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/ob219/public_html/three/list_item.php on line 10

My **list_item.php** code:

 <?php
include 'library/connect.php';

$book_id =$_GET['id'];

$result = mysql_query("SELECT * FROM Books WHERE book_id = '$book_id'");

echo "<table border = '1'><tr><th>Title</th><th>Author</th><th>Category</th></tr>";

    while($row = mysql_fetch_array($result)) // !!!problem here!!!
    {
        echo "<tr>";
        echo "<td>" .$row['Title']. "</td>";
        echo "<td>" .$row['Author']. "</td>";
        echo "<td>" .$row['Category']. "</td>";
        echo "</tr>";
    }
echo "</table>";
    include 'library/close.php';
    ?>
    <a href="mysql_con_links.php">Back</a>

my **mysql_con_links.php** code

    <?php
include 'library/connect.php';
    $result = mysql_query("SELECT * FROM `Books` ORDER BY  `Author` DESC");

echo "<table border='1'>
    <tr><th>Title</th><th>View Details</th></tr>";

    while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>".$row['Title']."</td>";
        ?>
        <td><a href="list_item.php?id=<? echo $row['book_id'] ?>">view</a></td>
        <?php 
        echo "</td>";
    }   

echo "</table>";
    include 'library/close.php';
    ?>
</div>
  • 写回答

2条回答 默认 最新

  • douwu3763 2013-12-06 13:57
    关注

    Your error is telling you are passing a boolean in to the mysql_fetch_array function. That says to me that the error lies in your $book_id variable. Most likely your mysql_query() function is returning "false".

    If I were you I'd make sure you're passing the correct type of variable to your mysql_query().

    You can either escape it with the mysql_real_escape_string function.

    $book_id = mysql_real_escape_string($_GET['id']);
    

    Or you could typecast the id to make sure getting a true integer (if your id is an integer).

    $book_id = (int) $_GET['id'];
    

    Then after passing the data, remember you don't need to have the quotes when passing an integer to a MySQL query.

    $result = mysql_query("SELECT * FROM Books WHERE book_id = $book_id");
    

    Also make sure your $_GET['id'] is set. using the isset() function.

    p.s. I'd move away from mysql_ functions, they are depreciated in later versions of php. Have a read up on PDO.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。