SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Row Count Help in PDO

    Hey there,

    I am trying to count the number of rows returned by a PDO. I have tried to use http://php.net/manual/en/pdostatement.rowcount.php (Example #2) to try and make my select query.

    here's the code

    PHP Code:

    $query
    ['1'] = 'SELECT COUNT(*) FROM users WHERE `email` = :email AND `password` = :password';

    $x $pdo->prepare($query['1']);
    $values['1'] = array('email' => $email'password' => $password);

    if(
    $res $x->execute($values['1'])){
            if(
    $res->fetchColumn() == 1){
                    
    $query['2'] = 'SELECT * FROM users WHERE email= :email AND password= :password';
                    
    $y $pdo->prepare($query['2']);
                    
    $values['2'] = array('email' => $email'password' => $password);
                    
                    
    session_start();
                    foreach(
    $y->execute($values['2']) as $row)
                        {
                            
    $_SESSION['id'] = $row['id'];
                            
    $_SESSION['email'] = $row['email'];
                            
    $_SESSION['name'] = $row['nameFirst'];
                        }
                echo 
    "<div class=\"sucess\">\n<p>You have been logged in. This page well redirect you to our home page in 5 seconds or less</p>/n</div>";
                    }
                }
                else{
                    echo 
    "<p class=\"loginError\">Your email or Password was wrong, please try again!</p>";
                    require(
    '../../views/forms/login.php');
                } 
    And i am getting an error on line 18 : Call to a member function fetchColumn() on a non-object in /var/www/ageasy/lib/account/login.php on line 18.

    Any help would be appreciated.

  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    From the example given by php.net (the link you provided), you should use the same reference variable as execute() so it should be this:

    $x->fetchColumn()

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Why the need to count the rows, check the count of rows is 1 (what if there is 2?) if so, then go back to the db and get the details.

    When in fact all you need to do is learn how to handle:

    If user/pass exists in the db then get their details otherwise fail.

    Is there some other reason why you need to count the rows prior to querying the db?


Bookmarks

Posting Permissions

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