5

This is quite strange, the query executes just fine and inserts the data into the table, but my affected rows shows on the negative side rather than 1. Does anyone know why this is happening?

connect.php:

<?php
 error_reporting(0);
 $db = new mysqli('localhost', 'root', 'pass', 'db');
 ?>

here is the main code:

<?php
 include 'connect.php';
 include 'blowfish.php';

 if($_POST['email'] == true)
 {
     if($db -> connect_errno)
     {
         echo "Could not connect to the database, please try again later...";
     }
     else
     {
         $email = $_POST['email'];

         $query = "SELECT id FROM users WHERE email = ?";
         $statmnt = $db -> prepare($query);
         $statmnt-> bind_param("s",$email);
         $statmnt-> execute();
         $statmnt-> bind_result($result);
         $statmnt-> fetch();
         $statmnt-> reset();
         if($result)
         {
             $id = $result;
             $result = "";

             $randomString = generateRandomString(50); // Generate random string

             $query = "UPDATE users SET emailString = ? WHERE id =?";
             $statmnt= $db -> prepare($query);
             $statmnt-> bind_param("si",$randomString,$id);
             $statmnt-> execute();
             $statmnt-> bind_result($result);
             $statmnt-> fetch();
             $statmnt-> reset();

             $test = $db -> affected_rows;
             echo $test;
         }
         else
         {
             echo "That Email Address is not registered...";
         }
     }
 }
 else
 {
     echo "No Email received...";
 }

 ?>

$test returns "-1"

2
  • 5
    "An integer greater than zero indicates the number of rows affected or retrieved. Zero indicates that no records were updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed. -1 indicates that the query returned an error." (Source)
    – dan-lee
    Sep 18, 2014 at 7:53
  • What kind of result are you expecting from an UPDATE query and why are you binding it then?
    – deceze
    Sep 18, 2014 at 7:54

1 Answer 1

3

You should get affected_rows value before you call reset() or close() function on $statmnt.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.