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"

Pramod
  • 2,828
  • 6
  • 31
  • 40
Dylan de St Pern
  • 469
  • 1
  • 7
  • 20
  • 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](http://php.net/manual/en/mysqli.affected-rows.php)) – dan-lee Sep 18 '14 at 07:53
  • What kind of result are you expecting from an `UPDATE` query and why are you binding it then? – deceze Sep 18 '14 at 07:54

1 Answers1

3

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

NatalyaKst
  • 315
  • 3
  • 6