Search code examples
phpmysqlisql-updateprepared-statementmysql-num-rows

PHP- mysqli->num_rows always returns 0, Prepared statements


First of all I would like to tell that I have already gone though ALL DUPLICATE questions. and tried the changes suggested there.

As far as now I have already tried changing num_rows to num_rows() And using store_result(); And using affected_rows().

Also calling store_result(); after execute()

I think there might be some other problem which I can't figure out

$conn->autocommit(false);   
if ($sucess){
    $stmt2 = $conn->prepare("UPDATE e_eventqueue SET e_urlfil=? WHERE e_id=? 
AND u_id=?");
    $stmt2->bind_param("iis",$e_urlfil,$e_id,$u_id);

    if($stmt2->execute()){
         $stmt2->store_result();
        echo "true";
        echo $stmt2->num_rows;  // <- this always return 0 even when its not
        $stmt2->close();
        $conn->commit();
    }
 else{
      $conn->rollback();
     echo "rollback";
  }

}

Solution

  • 1st : For update query You need to check affected_rows . not num_rows

    2nd : After execute check like this $row_count= $stmt2->affected_rows; If query executed successfully but no changes in data means it will return 0 .

    if($stmt2->execute()){
    
        echo $stmt2->affected_rows; 
     }
    

    Affected_rows :

    Affected_rows is for insert,update,delete

    Num_rows :

    Num_rows is for select