简体   繁体   中英

PDO row count confusion

I am making a login page that checks for matching values in a database if the SELECT query returns a matching row with username and password then it will return a row count of 1. The way I have it coded right now when I echo the variable that stores the row count it will echo 26 for some reason and I'm not to sure why.

Would someone explain if I am doing something wrong or if this is normal behavior and where that value is coming from?

function checkLogin($conn,$myusername,$mypassword,$row,$row1){
   try {  
        $sql = "SELECT COUNT(*) FROM CLL_users WHERE user_name = 'user' AND password = 'XXXX'";
        if ($results = $conn->query($sql)) {

            if($results->fetchColumn() > 0) {

                $sql = "SELECT * FROM CLL_users WHERE user_name = 'user' AND password = 'XXXXX'";
                foreach ($conn->query($sql) as $row)
                {               
                   $rowCount = count($row);
                   echo $rowCount;
                    print ("Username: " . $row['user_name'] . "<br>");
                     print ("Username: " . $row['password'] . "<br>");

                    }

                    echo $count;
          }
        else {
            print "NO ROWS";
        }
        }
        } catch (PDOException $e){
    echo 'Connection failed: ' . $e->getMessage();
}

}

Your code, $rowCount = count($row); , is counting the columns in the current row - not the number of rows returned by the query.

On the same note, you are echoing a second count related variable, $count , but you neither declare-it nor increment it in your code. It looks like this one is the one that's supposed to be counting the number of rows you loop through. If this is true, you should set it as $count = 0; before the loop and use $count++; within it:

$count = 0;
foreach ($conn->query($sql) as $row) {
    print ("Username: " . $row['user_name'] . "<br>");
    print ("Username: " . $row['password'] . "<br>");
    $count++;
}
echo $count;

Also, you're currently using PDO's rowCount prior to selecting a user, and you're using it properly. You could just store that result into a variable and use it to tell how many rows you are receiving:

$sql = "SELECT COUNT(*) FROM CLL_users WHERE user_name = 'user' AND password = 'XXXX'";
if ($results = $conn->query($sql)) {
    $numRows = $results->fetchColumn();
    if($numRows > 0) {
        ... rest of your code ....
function checkLogin($conn,$myusername,$mypassword,$row,$row1)
{
   try
   {  
       $sql = "SELECT COUNT(*) FROM CLL_users WHERE user_name = 'user' AND password = 'XXXX'";
       if ($results = $conn->query($sql))
       {

           $count = $results->fetchColumn();
           echo "$count\n";

           if($count > 0)
           {
               $sql = "SELECT * FROM CLL_users WHERE user_name = 'user' AND password = 'XXXXX'";
               foreach ($conn->query($sql) as $row)
               {               
                   print ("Username: " . $row['user_name'] . "<br>");
                   print ("Username: " . $row['password'] . "<br>");     
               }
           }
           else
           {
               print "NO ROWS";
           }
       }
   }
   catch (PDOException $e)
   {
       echo 'Connection failed: ' . $e->getMessage();
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM