SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: PDO usage

  1. #1
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PDO usage

    Hi all,

    I'm still trying to get a full grasp on PDO.

    Here's what I'm working on:

    PHP Code:
    $sql $dbh->prepare("SELECT name, address1, address2, city, state_prov, logo, url FROM stores WHERE storecode = ? ");
    $sql->execute(array($code));
    $rows $sql->fetchAll();
    $num count($rows);
    if(
    $num !== 1)
    {    
        
    $errors[] = 'A valid code is required to continue.';
        
    $status '<ul class="errors">';
        foreach(
    $errors as $e)
        {
            
    $status .= "<li>$e</li>";
        }
        
    $status .= '</ul>';
    }else{
        
    $row $rows[0];
        
    $name $row["name"];
        echo 
    $name;

    It works (as in displaying the correct name), but I'm just wondering if there's a better way of doing it. Thanks for any pointers!
    No, I REALLY dislike having to use Joomla.

  2. #2
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a recent post with some examples and suggestions.

    http://www.sitepoint.com/forums/showthread.php?t=612080

    As well, the PDO documentation, in case you didn't have it.

  3. #3
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to get the last inserted id.

    PHP Code:
    $insert $dbh->prepare("INSERT INTO customers (email) VALUES (?)");
    $insert->execute(array($email));
    $userID $insert->lastInsertId(); 
    I get this error:
    Call to undefined method PDOStatement::lastInsertId()
    I'm trying to follow an example from the manual. Why would that method be undefined?
    No, I REALLY dislike having to use Joomla.

  4. #4
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay, so I changed it to:

    PHP Code:
    $insert $dbh->prepare("INSERT INTO customers (email) VALUES (?)");
    $insert->execute(array($email));
    $userID $dbh->lastInsertId(); 
    and it worked. I don't really understand why though. Could I get some wisdom, please?
    No, I REALLY dislike having to use Joomla.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    The variable $insert is the statement object. You must retrieve the last insert id though the connection. The variable $dbh is the PDO connection. Thus $dbh->lastInsertId() returns the most recent primary key created from that connection.


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
  •