phpmysqlinullreturn-valuemysql-num-rows

Mysqli num_rows() returns nothing/null after executing INSERT query


I´ve read many topics with the same problem, but neither of any solved my problem.
I´ve got following code:

$connection = mysqli_connect('...');
if(mysqli_connect_errno()) {
    die('Connect Error');
}
$connection->set_charset("utf8");
$success = $connection->query("INSERT INTO hilde_entrys (Comment, Refer_ID, Account_Adress) VALUES ('".$_POST["comment"]."','".$GET["id"]."','".$userData["user_address"]."')");
$success->store_result(); //I did this as it helped some people - it didn´t help me :(
$rows = $success->num_rows;
while($row = $success->fetch_row()) { //This whole while-loop helped some other guy on stackoverflow - not me
    $rows = $success->num_rows; //Incrementing by 1 each time
} 
$rows = $success->num_rows; // Finally the total count 

if ($rows > 0) { //What I actually intended to do with the num_rows...
    $success->free();
    $success->close();
    $connection->close();
} else {
    $success->free();
    $success->close();
    $connection->close();
    die("Query failed.");
}

Somehow, even though I tried everything possible, it just won´t return anything expected. (Yes, the query and everything else works. I tried it.). What is wrong?
Thank you for all your answers in advance,
VicStudio

Oh, btw: Even when i remove $success->free(); it doesn´t change anything with num_rows....

EDIT: After you guys told me that INSERT won´t return any rows, how can i then check if the query succeeded?

ONE MORE EDIT: First, someone told me to use affected_rows instead of num_rows, which I can todally understand. Somehow, it still didn´t work, so one of you asked me to activate error reporting in my php-script, and I now got following two error messages (one notice and one fatal-error):
Notice: Trying to get property of non-object in (my webpage) on line XX
Fatal error: Call to a member function close() on a non-object in (my webpage) on line XX
The notice refers to the line $rows = $success->affected_rows; (I replaced $rows = $success->num_rows with this). (I commented the while-loop away).
The second error, causing the screen to go blank, is referring to $success->close();, the one in the else-clause. This means
a) it doesn´t detect THAT something was changed and
b) that it has a problem with me closing the query.
Could somebody explain to me why? I checked my database and the INSERT-query succeeded, therefore it should have gone in the if, not in the else. And why doesn´t it want me closing the query? I assume both of these errors have something todo with eachother. Thanks in advance, guys. Sorry for the long time in between my new post, but it takes time figuring stuff out and especially writing it down.


Solution

  • As an mysqli_query() running an INSERT query only returned either TRUE or FALSE then your $success will never be a mysqli_result object as it would in a SELECT Query.

    Therefore your test for affected_rows should use the connection object like this. In fact the affected_rows property only even exists on the mysqli_object.

    $connection->affected_rows;
    

    So

    $connection = mysqli_connect('...');
    if(mysqli_connect_errno()) {
        die('Connect Error');
    }
    $connection->set_charset("utf8");
    $success = $connection->query("AN INSERT QUERY");
    
    $rows = $connection->affected_rows;
    

    The PHP manual aint great in this area I admit, but it does state this

    This also explains why

    $success->free();
    $success->close();
    

    dont work as $success is not an object.

    And also why

    $success->store_result();
    

    does not work. $success is not an object and there are no results to store.