3

while performing inner join when i add data in mysql it shows a error mysql_fetch_array is not a valid argument. code is below:

    echo $sql="SELECT * FROM info
 INNER JOIN item ON info.info_id=item.info_id where item.info_id=".$info_id;
        $query=mysql_query($sql);
         while($result=mysql_fetch_array($query))
        { 
             echo "<tr>";
             echo "<td>" .$result['Name']. "</td>";
        echo "<td>" .$result['Item']. "</td>";
         echo "<td>" .$result['Price']. "</td>";
        echo "<td>" .$result['info_id']. "</td>";
             echo "<td><a href='item.php?act=edit&id=".$result['id']."'>Edit</a></td>";
             echo "<td><a href='item.php?act=delete&id=".$result['id']."'>Delete</a></td>";
             echo "<td><a href='item.php?act=item&id=".$result['id']."'>Item</a></td>";
echo "</tr>";

while running the above code it shows the following warning:

warning:mysql_fetch_array(): supplied argument is not a valid MySQL result resourc

user39891
  • 31
  • 2
  • Check your query that returns anything. I think it returns false – Edwin Alex Apr 08 '13 at 10:39
  • 1
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are probably also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Apr 08 '13 at 10:40
  • Please share the creating connection code, are you selecting the database? One thing important is `mysql` api is deprecated use `mysqli` instead. – Muhammad Ummar Apr 08 '13 at 10:40
  • check `echo mysql_num_rows($query);` before ur `fetch` statement – Suhel Meman Apr 08 '13 at 10:41
  • @suhel - an empty result set wouldn't give that warning – Mark Baker Apr 08 '13 at 10:42
  • @Quentin is right, you should probably not be using mysql_query as it is deprecated ([starting from PHP 5.5.0](http://php.net/manual/en/function.mysql-query.php)) – Erik Schierboom Apr 08 '13 at 10:46
  • try checking error `$query=mysql_query($sql) or die(mysql_error())`..I think it should return you with some thing useful..... and I suggest you start using mysqli_* functions – alwaysLearn Apr 08 '13 at 10:47
  • @Ummar i have created connection with the database, and the connection is successfully established. **mysql_connect('localhost','root','root') or die('oops error in connection'); mysql_select_db('friend_list') or die('error in selecting db');** – user39891 Apr 09 '13 at 10:06

1 Answers1

0

The mysql_query method probably fails silently. If it does, it returns FALSE so you should be doing this:

$query=mysql_query($sql);
if (!$query) {
    die('MSSQL error: ' . mssql_get_last_message());
}
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81