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 8505055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:12:40+00:00 2026-06-11T02:12:40+00:00

trying to convert all my old mysql_* operations into new and, from what i’ve

  • 0

trying to convert all my old mysql_* operations into new and, from what i’ve heard, improved PDO, but this query wont seem to run successfully, I am trying to select all from the table PEOPLE where the username = $username (which has previously been declared $username = $_SESSION['username'];)

$query = "SELECT  * FROM people WHERE username=?";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $username);   
$stmt->execute();
$num_rows = $stmt->fetchColumn();

if ($num_rows == 1) {
    // ...
}

THE WORKING CODE IS:

$query = "SELECT  * FROM people 
             WHERE username=?";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $username);   
$stmt->execute();
$num_rows = $stmt->fetchColumn();
$user = $stmt->fetchObject();

if ($user) {

//do something

}
  • 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-11T02:12:42+00:00Added an answer on June 11, 2026 at 2:12 am

    $stmt->fetchColumn does not fetch the number of rows; in this case it will fetch the first column from the first row of the result set. Since that will not be equal to 1 generally your test will fail.

    In this case there is also no real need to count the number of returned rows because you are expecting either one or zero (if the username does not exist). So you can simply do:

    $stmt->execute();
    $user = $stmt->fetchObject();
    if (!$user) {
        // not found
    }
    else {
        echo "User $user->username found!";
    }
    

    The if(!$user) test works because if there is no row to fetch $user will be false (see the documentation for fetchObject).

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

Sidebar

Related Questions

I'm trying to convert all my old javascripts to jquery but I'm a bit
I'm trying convert all special chars into HTML safe entities on their way into
I'm trying to convert some simple .htaccess rewrites into web.config xml; all is well
Trying to convert this c code into MIPS and run it in SPIM. int
I'm trying to convert the data from a simple object graph into a dictionary.
ALL, I am trying to convert Borland C++ code to C#. In the old
Im new to jQuery and i'm trying to convert all my phone class to
We are trying to convert out old school client server application into a N-Tier
I'm trying to convert all .php files to run with .html extensions but it
I'm trying to convert old text in ascii non-english font to new unicode font.

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.