-2

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

On my detailed view page, and grabs the correct ID in the URL bar, like so:

http://localhost/attractions/details.php?ID=1

My page title and sidebar load, but in the space where I want my details of the record, I get this error message:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/attractions/details.php on line 41

This is the 'details.php' page code (with the unnecessary html removed):

<?php
include 'page-start.php';
$myQuery  = "SELECT Attraction.*, Type.TypeName ";
$myQuery .= "FROM Attraction ";
$myQuery .= "INNER JOIN Type ON Attraction.Type = Type.TypeID";
$myQuery .= "WHERE AttractionID=" . $_GET['ID'];
$result = mysql_query($myQuery);
if (!result) {
    die('Query error: ' . mysql_error());
} 
?>

<body>
<div class="container">
        <h2>List of Attractions</h2>

        <?php
            //display attractions row by row
            while($row = mysql_fetch_array($result))
            {
                echo '<div class="attraction">';
                echo '<h4><a href="details.php?ID=' . $row['AttractionID'] . '">' . $row['Name'] . '</a></h4>';
                echo '<div class="featureimage">';
                echo '<img src="'. $row['ImageUrl'] . '" />';
                echo '</div>';
                echo '<p>' . $row['TypeName'] . '</p>';
                echo '<h5>' . $row['Summary'] . '</h5>';
                echo '</div>';  
            }
        ?>
</div><!-- container -->
</body>
</html>
<?php
include 'page-end.php';
?>

Line 41 is this line:

while($row = mysql_fetch_array($result))

I just know I will be overlooking something so simple here, but I'm stumped and I really cannot figure out what's causing the error, please can some one shine a little light on my situation, I've searched SO for similar questions, but there were none that could help me!

Community
  • 1
  • 1
Jon Kyte
  • 1,878
  • 3
  • 25
  • 39
  • http://localhost/attractions/details.php?ID=1 obviously wouldn't work ;) – Phorce Nov 20 '12 at 18:20
  • You are missing a `$` in `if (!result)`. – deceze Nov 20 '12 at 18:23
  • You're also vulnerable to **SQL injection**. See http://kunststube.net/escapism. – deceze Nov 20 '12 at 18:24
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained and the [deprecation process](http://j.mp/Rj2iVR) has begun on it. See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – tereško Nov 20 '12 at 18:25

4 Answers4

2

your missing backticks as required in MySQL, for the Join code, when your using table names that are keywords E.G Type | TYPE | TyPE...

$myQuery  = "SELECT `Attraction`.*, `Type`.`TypeName` ";
$myQuery .= "FROM `Attraction` ";
$myQuery .= "INNER JOIN `Type` ON `Attraction`.`Type` = `Type`.`TypeID`";
$myQuery .= "WHERE `AttractionID` =" . $_GET['ID'];

and as Jeffrey said it will return false if the query was invalid

Barkermn01
  • 6,560
  • 32
  • 80
  • He would have caught that if he had written $result instead of result. See my answer. – Derk Arts Nov 20 '12 at 18:26
  • @saratis your just providing code for what jeffrey already said and has now update his code to show – Barkermn01 Nov 20 '12 at 18:27
  • @saratis lol its not a competition. – itachi Nov 20 '12 at 18:33
  • The missing '$' on result was just a simple oversight on my part, the backticks were a huge oversight, and adding them rectified this and gave me what I was looking for, so marked this as correct - Thanks Martin. – Jon Kyte Nov 20 '12 at 18:36
  • Just to elaborat if you corrected the $result problem you still my not have known backticks were needed from the MySQL error as all it would have said is `syntax error near Type.TypeName FROM Attraction INNER JOIN Type` MySQL's Syntax error's are not of much help – Barkermn01 Apr 12 '14 at 21:16
2

Notice that mysql_query() may return boolean values. You need to check whatever mysql_query() worked or not (if not it returns false) before using its results as result type.

In your case the query obviously failed and returned false that can't be used as resource. You should go with

if (!$result) { /* we have a database error */ }
Shoe
  • 72,892
  • 33
  • 161
  • 264
0

Look at this part: if (!result) { your missing the $. That should generate an error by itself, maybe you are not showing notices. Anyway, your SQL is returning false, but you're not catching that because of the error I mentioned. So fix that error and check your SQL.

Derk Arts
  • 3,372
  • 2
  • 17
  • 36
0

The likely explanation if that your mysql_query is simply failing, giving your variable $result a value of FALSE (which is a boolean) and then mysql_fetch_array() is choking on that BOOLEAN. Verify that your query works. You're not triggering your Query Error condition because you're missing the $ on if (!result) {

Also, best practice it to use PDO or MYSQLI rather than mysql_ functions. (They're (going to be--thanks Martin) deprecated.)

David Hayes
  • 151
  • 5
  • Hey there not deprecated yet as they would raise an E_DEPRECATED notice/Log there just discouraged they wont Deprecate them for a long time as to many system use them and don't want to be logging E_DEPRECATED notices in the php log – Barkermn01 Nov 20 '12 at 18:34