Unknown developer
Unknown developer

Reputation: 5950

php affected_rows seems not to work

Could someone,please, explain what's the problem with the following code?

<?php  
  $db=new mysqli('localhost', 'root','','apeirosto');          
  $query="UPDATE  INBOX SET autodelete=1 WHERE messageid=129";
  $result=$db->query($query);
  echo $result->affected_rows; 
?> 

Table INBOX has messageid as its primary key and autodeleteis one of its fields. Whereas everything is ok with UPDATE echo does not return anything. The same problem happens with :

if ($result->affected_rows==0) 

Which always returns true! I cannot understand...

Upvotes: 2

Views: 195

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

That isn't how affected_rows() work.

You need to pass the DB connection variable to it

$db->affected_rows;

Read the manual http://php.net/manual/en/mysqli.affected-rows.php

Object oriented style

int $mysqli->affected_rows;

Example pulled from the manual

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
...
$mysqli->query("UPDATE Language SET Status=1 WHERE Percentage > 50");
printf("Affected rows (UPDATE): %d\n", $mysqli->affected_rows);

Upvotes: 1

Related Questions