Search code examples
mysqlsqlphp-5.2

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 21


Ok below is my code and I've tried to debug it with var_dumps .. var_dump($insertion) returns through , which means that the query inserts into the database but for some reason , I'll get the warning message when I wanna see if mysql_num_rows is doing its job and it returns FALSE with the warning message of the title .. Below is my short straight-forward code

<?php

require'sensdb.php';

if (sensdb_connect()) {

$select = "SELECT SubGroupID from STx WHERE GroupID = '39'AND Status ='Provisioned'";

$result = mysql_query($select);
$result_count = mysql_num_rows($result);

  if($result_count > 0)
   {
       while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {


         if($row['SubGroupID'] != 123) {

        $insert = "INSERT INTO Routes (SubGroupID,GroupID,URL) VALUES (".$row['SubGroupID'].",39,'mailto://[email protected]')";
        $insertion = mysql_query($insert);
        $insertion_count =  mysql_num_rows($insertion); var_dump($insertion_count);
        $select_route_id = "SELECT RouteID FROM Routes WHERE SubGroupID =".$row['SubGroupID']." AND GroupID = 39";
        $result_route_id = mysql_query($select_route_id);
        $result_route_count = mysql_num_rows($result_route_id);
         if($insertion_count > 0 ) {
         if($result_route_count > 0) {
            $row_route = mysql_fetch_array($result_route_id,MYSQL_ASSOC);
             $update = "UPDATE STx SET RouteID =".$row_route['RouteID']." WHERE SubGroupID =".$row['SubGroupID']."";
             var_dump($update);

             }

           }

         }

     }

  }

}

Solution

  • From the PHP docs:

    Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().

    PHP.net

    Also, you should really be using PDO (or something like it).