Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: PDO select

  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    PDO select

    Hi,
    i was wondering if this is the right way to select from the database using pdo. Also should this line be at the end or no $row = $stmt->fetch();. Thanks.

    PHP Code:
     $query "
                SELECT
                    id,
                    username,
                    password,
                    salt
                FROM users
                WHERE
                    username = :username
            "
    ;
            
            
    $query_params = array(
                
    ':username' => $_POST['username']
            );
            
            try
            {
                
    $stmt $db->prepare($query);
                
    $result $stmt->execute($query_params);
            }
            catch(
    PDOException $ex)
            {

                die(
    "Failed to run query: " $ex->getMessage());
            }
            
            
    $row $stmt->fetch(); 

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    without one of the fetch methods you have no access to the data.

    PS. the try...catch block only makes sense if you set PDO’s error handling to Exceptions
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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