MySql_fetch_array() error in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dslade
    New Member
    • Mar 2007
    • 1

    MySql_fetch_array() error in PHP

    I got the following message when trying to do a mysql php query:

    Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apa....

    Can anyone help offer suggestions?

    Here is the code:
    $query = "SELECT title, role_name, CONCAT(first_na me, ' ', last_name) AS name FROM actors, roles, movies WHERE actors.act_id = roles.act_id AND roles.movie_id = movies.movie_id AND year > 1999 ORDER BY year;";

    $result = @mysql_query ($query);
    // Print the results
    echo '<table><tr><t d align="left"><b >Movie Title </b></td><td align="left"><b >Role Name </b></td><td align="left"><b >Actor</b></td></tr>';
    while ($row = mysql_fetch_arr ay
    ($result, MYSQL_ASSOC))
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Next time please enclose your php lines with PHP tags :)

    Since i don't have your Table structure i didn't check the SQL Query.
    If this is not working post the SQL script with some sample values.
    then i can check it in my end.

    [PHP]<?php
    $query = "SELECT title, role_name, CONCAT(first_na me, ' ', last_name) AS name FROM actors, roles, movies WHERE actors.act_id = roles.act_id AND roles.movie_id = movies.movie_id AND year > 1999 ORDER BY year;";

    $result = mysql_query($qu ery);
    // Print the results
    echo '<table><tr><t d align="left"><b >Movie Title </b></td><td align="left"><b >Role Name </b></td><td align="left"><b >Actor</b></td></tr>';
    while ($row = mysql_fetch_arr ay($result))
    {
    echo '<tr>
    <td align="left">'. $row['title'].'</td><td align="left">'. $row['role_name'].'</td><td align="left">'. $row['name'].'</td>
    </tr>';
    }
    echo '<table>';
    ?>[/PHP]

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      Get rid of the idiotic '@' symbol then you might see why it is failing. As the error occurs here
      Code:
      $result = @mysql_query ($query);
      . Your query is wrong. We don't know why without your table

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        You suffer from the same shortcoming many developers have: you must always check the results of any database or file related command!

        In this case the problem is that your query returns an error, so you must capture that error before you continue. Like this:
        Code:
        $result = mysql_query ($query) 
            or die("Query error: " . mysql_error());
        Ronald :cool:

        Comment

        Working...