Welcome Guest, Not a member yet? Register   Sign In
ajax won't let me obtain affected_rows
#1

(This post was last modified: 04-23-2016, 11:28 PM by cupboy.)

This code is in a file being run as ajax. Trying to obtain the count of affected rows causes a failure. I get no error message, and the try .. catch doesn't catch it either. What could be going on? I'll blame it on the ajax since code like this works everyplace else.
Also this returns false: var_dump(method_exists($q,'affected_rows')); // returns false
Can that be? Wouldn't a query always have that method? Or isn't it a method?


Code:
 $q = $mysqli->query($sql);
 if (is_object($q))
   echo "this is as far as it goes.............";
 else
   echo "not object";
 $rowsAffected = $q->affected_rows(); // <== this is where it fails
 echo "rows returned: ";  // .$rowsAffected; // fails on this and says nothing about it.
 return true;
Reply
#2

If you are choosing OOP style for mysqli query then you have to use mysqli object to use any method/property of mysqli.

TRY:
$rowsAffected = $mysqli->affected_rows;

affected_rows is a property of mysqli object and not query. You can refer http://php.net/manual/en/mysqli.affected-rows.php for more information.
Reply
#3

(04-24-2016, 01:18 AM)raghavgarg Wrote: If you are choosing OOP style for mysqli query then you have to use mysqli object to use any method/property of mysqli.

TRY:
$rowsAffected = $mysqli->affected_rows;

affected_rows is a property of mysqli object and not query. You can refer http://php.net/manual/en/mysqli.affected-rows.php for more information.
That looks like how I am already doing it. I've tried it with and without those parenthesis on the end.
Reply
#4

(This post was last modified: 04-25-2016, 01:02 PM by raghavgarg.)

(04-24-2016, 10:01 AM)cupboy Wrote:
(04-24-2016, 01:18 AM)raghavgarg Wrote: If you are choosing OOP style for mysqli query then you have to use mysqli object to use any method/property of mysqli.

TRY:
$rowsAffected = $mysqli->affected_rows;

affected_rows is a property of mysqli object and not query. You can refer http://php.net/manual/en/mysqli.affected-rows.php for more information.
That looks like how I am already doing it. I've tried it with and without those parenthesis on the end.

Sorry, if I was not clear. I wasn't only talking about parenthesis, my main emphasis was on the variable you are using before that

You are using 
$q = $mysqli->query($sql);
$rowsAffected = $q->affected_rows();
                ^^
I am talking about this variable, this should be "$mysqli" and not "$q" because the property you want to use is of the "mysqli" object.

The final code should be like:
$mysqli = new mysqli($server, $user, $password, $db_name);
$q = $mysqli->query($sql);

$rowsAffected = $mysqli->affected_rows;
echo $rowsAffected;
Reply




Theme © iAndrew 2016 - Forum software by © MyBB