SitePoint Sponsor

User Tag List

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

    undefined object + PDO

    hey there,

    I'm currently getting an undefined object fetchall error and PDO selection script. because from what I can see the PDO variable is being defined. But maybe that's just me. There's a good chance that I'm missing something.

    In a config file. I have defined all variables needed and started the PDO connection as the variable $pdo.

    and here's the section of code that is giving me the grief

    PHP Code:

    $queryselectemail 
    "SELECT COUNT(*) FROM users WHERE email = :email";

                
    $values =  array('email' => $email);

                try {
                    
    $tryemail $pdo->prepare($queryselectemail);
                    
    $tryemail->execute($values);
                
                } catch (
    PDOException $e) {

                    
    $title "There was an error :(";
                    
    $output "We are sorry but there has been an error! The error is a PDO/Mysql error and it is {$e->getMessage()}";
                }
            }

                if(
    $tryemail->fetchAll() < 1){
                                    
    /* code to insert user data */

    however I am just getting the error. I made if the e-mail already exists in the database. (Not shown above)

    thanks in advance.

  2. #2
    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)
    PHP Code:
    $values =  array(':email' => $email); 
    I think ... missing colon

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Nope. I don't need a colon in the array.

    If i was to do it like
    PHP Code:
     $pdo->bindValue(':email'$email
    you would need the colon. But done in a array you do not. OR at least from all the books i have read that is what i go out any ways.

  4. #4
    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)
    oops... sorry.

    When you say only getting the error, do you mean you are getting an exception?

    Have you included the config file?

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    oops... sorry.

    When you say only getting the error, do you mean you are getting an exception?

    Have you included the config file?
    No problem man!

    No it's an "Email allready in use" 'error'. Yes the config file is being included as up to that part is executing just fine.

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, I think this is a workflow issue, you will always have 1 row returned by your query because you are doing a COUNT(*). For a non-matching e-mail, the row will have a value of 0, for a matching e-mail, it will have a value of 1 (either way, you get 1 row returned with 1 value).

    The second issue I see, is your fetchAll is assuming it returns an int value, it returns an array, so you might have to use $tryemail->fetchAll()[0][0] (signifies 1st row and 1st column. or if PHP doesn't support that syntax the following may be the correct approach)

    PHP Code:
    $rows $tryemail->fetchAll();
    if (
    $rows[0][0] < 1)
    {
       
    // insert user data 

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    for got to post. But i got it resolved. What i eneded up doing was if what was retured was more then one i ened saying the email was in use. then in the else state that is where i insert the data.

    Thanks for the help though


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
  •