Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2014
    Posts
    129
    Thanks
    39
    Thanked 1 Time in 1 Post

    mysqli_affected_rows

    Hello

    Following problem:
    I update a database (which works) and then I want to know how many rows have been updated.
    PHP Code:
    $update "UPDATE yout SET run = 'yes' WHERE ID = '9' " ;
    $result mysqli_query($db$update); 
    $count mysqli_affected_rows($update); 
    I got the message:
    Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, string given

    Do anybody knows where the problem is?
    Thanks for helping

  2. #2
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,329
    Thanks
    4
    Thanked 480 Times in 468 Posts
    You pass it $result, not $update.

    This is another of the MANY reasons I say if you're going to use mysqli, ditch the outmoded outdated silly decade out of date code bloat overhead inducing procedural model. The function wrappers for mysqli do nothing more than introduce extra processing time for nothing of value.

    I'd also suggest kicking the "variable for nothing" to the curb...

    OH, and ->query doesn't return mysqli_stmt, it returns mysqli_result, which has num_rows not affected_rows. ANOTHER of the many reasons I say use PDO instead of mysqli since it returns the same object type regardless of what operation you run. mysqli makes a confusing mess out of it by having ->prepare and ->query return two entirely different object types for result handling.... because reasons?

    Code:
    $result = $db->query('
    	UPDATE yout
    	SET run = `yes`
    	WHERE ID = 9
    ');
    $count = $result->num_rows;
    Real world I would also kick that $count "variable for nothing" right square in the junk and just use $result->num_rows.

    Basically if you use ->prepare
    PHP: mysqli:repare - Manual

    It returns mysqli_stmt which has ->affected_rows
    PHP: mysqli_stmt - Manual

    But when you use ->query
    PHP: mysqli::query - Manual

    It returns either false (failure) or mysqli_result
    PHP: mysqli_result - Manual

    Which does not have ->affected_rows, it has ->num_rows.

    ... and in either case you use the returned object, not your query string for accessing results.

    Though again, if you're not too deep into whatever you are building, I'd also give a good swift boot to mysqli altogether and convert over to PDO. With PDO ->query, ->exec, and ->prepare all return PDO_statement meaning it's the same interface regardless of what you're doing. Said interface to accessing results also being far simpler and easier to work with than the mentally enfeebled train wreck that is mysqli.
    Last edited by deathshadow; Jan 26th, 2019 at 07:20 PM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  3. #3
    Regular Coder
    Join Date
    Dec 2014
    Posts
    129
    Thanks
    39
    Thanked 1 Time in 1 Post
    Thanks a lot. I took your code. The update was made (from 'no' to 'yes')
    PHP Code:
    $count $result->num_rows;
    var_dump ($count); 
    and it's NULL - Why? I can see that the db-table has been updated, but why $count is NULL? ?

  4. #4
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,955
    Thanks
    36
    Thanked 1,064 Times in 1,060 Posts
    NULL would mean it don't exist. And you added something: the num_rows, maybe your thinking $mysqli_result->num_rows;
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  5. #5
    Regular Coder
    Join Date
    Dec 2014
    Posts
    129
    Thanks
    39
    Thanked 1 Time in 1 Post
    YES! Thanks a lot for your help!


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •