什么会导致传递给mysql_fetch_object()的资源无效? [关闭]

I got a warning in php as follows

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in E:\Program Files\VertrigoSer\www\b.php on line 16

My program is:

<?php
$connection=mysql_connect("localhost","root","vertrigo");
$db=mysql_select_db("mydb",$connection);

$query="INSERT INTO text VALUES(1003,'PHP','Mcgrowhill','Ivan')";
$r=mysql_query($query);

$query="INSERT INTO book VALUES(201,3,1003)";
$r=mysql_query($query);

$query1="select distinct title from text,book,course where text.isbn=book.isbn and book.cno=course.cno and course.dept='CS'";
$r=mysql_query($query1);

echo"row inserted";

while($row=mysql_fetch_object($r))
{
    echo $row->title;
}

mysql_close($connection);
?>

kindly give suggestions that how to solve this problem

Here is how you can find out:

$query = "INSERT INTO text VALUES(1003,'PHP','Mcgrowhill','Ivan')";
$r = mysql_query($query);
if (!$r) {
    echo mysql_error();
}

$query = "INSERT INTO book VALUES(201,3,1003)";
$r = mysql_query($query);
if (!$r) {
    echo mysql_error();
}

$query1 = "select distinct title from text, book, course where text.isbn = book.isbn and book.cno = course.cno and course.dept = 'CS'";
$r = mysql_query($query1);
if (!$r) {
    echo mysql_error();
} else {
    echo"row inserted";

    while($row = mysql_fetch_object($r)) {
        echo $row->title;
    }
}

mysql_close($connection); ?>

If the SQL query returns no result, it could trigger this warning. Before doing the following,

$r=mysql_query($query1);

Check if number of rows returned is > 0 with the mysql_num_rows function. Also check your query directly to see if it works.

Stop using the MySQL extension, it is discouraged. Use PDO or MySQLi

You can use something like this.

error_reporting(E_ERROR | E_PARSE);

Or

if (!$result)
   die("mySQL error: ". mysql_error()); 

Text is a Reserved word of mysql so use alias and try it

Like

$query1="select distinct title from text as t,book as b,course as c where t.isbn=b.isbn and b.cno=c.cno and c.dept='CS'";