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>

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.

mysql_query returns false, if there was an error processing your query. You can see this error by calling mysql_error function.

I tried to improve your code, commenting my actions:

$book_id = $_GET['id'];
//Escape the variable before you pass it to MySQL:
$book_id = mysql_real_escape_string($book_id);

//remove quotes, if it's an integer value
$result = mysql_query("SELECT * FROM Books WHERE book_id = $book_id");

PS. Try to avoid using mysql_* functions, they are deprecated.