Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8512301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:14:30+00:00 2026-06-11T04:14:30+00:00

Should I be using mysql_num_rows ( rowCount in PDO ) in update or insert

  • 0

Should I be using mysql_num_rows (rowCount in PDO) in update or insert query?
Currently, my code looks likes this,

public function update_username(){
    $q = "UPDATE usertable SET username = '$user_name' WHERE id = '$user_id' LIMIT 1";
    $r = $db->query($q);
    if($r){
        $message = "Updated successfully";
        return $message;
    }else{
        return false;
    }
}

Should I change it to like this?

public function update_username(){
    $q = "UPDATE usertable SET username = '$user_name' WHERE id = '$user_id' LIMIT 1";
    $r = $db->query($q);
    if($r){
        $num = $r->rowCount();
        if($num == 1){
            $message = "Updated successfully";
            return $message;
        }else{
            $message = "An error occurred";
            return $message;
        }
    }else{
        return false;
    }
}

Normally, query goes through without any error, so I shouldn’t worry about it too much, but which one would be a better coding practice? Or do you suggest something else?

Thanks so much in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:14:32+00:00Added an answer on June 11, 2026 at 4:14 am

    Actually the two codes do something different.

    The first one will print “Update success” if the query was successfully executed. But a query can be successfully executed also without affecting any row, i.e. you have a WHERE statamenet that does not match. The second code will not print “Update success” if no rows were affected.

    Of course, if you’re sure that your WHERE statement has to match, you can use both codes without any difference and using the second one could help you to spot any potential bug, i.e. it doesn’t match and so something went wrong (probably the id was different from the one you expected).

    Generally, to answer your question, mysql_num_rows is needed only if you want to know how many lines were affected. It’s not mandatory at all to use it.

    So, it depends on what you want. Both are good, but they are different.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public function doesUserExist($u) { $this->dbConnect(); mysql_select_db($this->database); $sUser = mysql_real_escape_string($u); $query = SELECT username FROM
Right, perhaps I should be using the normal Python lists for this, but here
Time and time again I've read on Stackoverflow that I should be using PDO
So I saw an answer in another question saying that this should work: using
Unfortunately, I wrote a lot of code using mysql_query, mysql_fetch_array and mysql_num_rows calls. It's
I am trying to log in using this code : session_start(); require connect.php; $username
I've been using this code: $rawsql = SELECT * FROM _erc_foffices n INNER JOIN
I am getting segfault on this function. /** * Excutes the passed query and
Using PHP mysql_num_rows() returns 1 when running the query below on the table below
Using MySQL should I store time data as an Int which would be the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.