0

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

I used the conditional clause: `

$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb", $link);
$sql = "SELECT id, title, genre, format, year,FROM dvd WHERE title LIKE 'T%'";
$result = mysql_query($sql, $link);

?>`

However, I get an error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\moviesort.php on line 114

where 114 is this line: <?php while ($row = mysql_fetch_assoc($result))

the full code containing line 114 is as follow:

<ul class="grid">

<?php while ($row = mysql_fetch_assoc($result)) 

{ ?>

<li>

<p class="image">
    <a href="synopsis.php?id=<?php echo $row['id']; ?>">
        <img src="getImage.php?id=<?php echo $row['id']; ?>" alt="" width="175" height="200" />
    </a>
    </p>
    <p class="name"><?php echo $row['title']; ?></p>
    <p class="genre"><?php echo $row['genre']; ?></p>
    <p class="format"><?php echo $row['format']; ?></p>
        <p class="year"><?php echo $row['year']; ?></p>
    </li>
    <?php } // while  
    ?>
</ul>

anybody knows what did i do wrong?

Community
  • 1
  • 1
exxcellent
  • 639
  • 4
  • 11
  • 18

3 Answers3

3
$sql = "SELECT id, title, genre, format, year,FROM dvd WHERE title LIKE 'T%'";
  1. this query is wrong, a comma before FROM
  2. use mysql_error() to get exact error message
zerkms
  • 240,587
  • 65
  • 429
  • 525
3
if (!$result) {
    echo mysql_error($link);
}

http://www.php.net/mysql_error

Which will likely complain about invalid syntax near year,.

deceze
  • 491,798
  • 79
  • 706
  • 853
2

You've got an extra , in your query, causing a syntax error:

$sql = "SELECT id, title, genre, format, year,FROM dvd WHERE title LIKE 'T%'";
                                             ^--here

with some basic defensive programming techniques, you'd have seen this error. In particular, you're not checking for database failures.

$result = mysql_query($sql, $link);
if ($result === FALSE) {
    die(mysql_error());
}

Never EVER assume that a database operation completed. Even if the query string you're using is 100% syntactically correct, it can still fail for any number of other reasons.

Marc B
  • 348,685
  • 41
  • 398
  • 480