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 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2012
    Location
    Finland
    Posts
    23
    Thanks
    2
    Thanked 5 Times in 5 Posts

    Help with PHP PDO

    Hey guys I just started to learn PHP PDO and I'm kinda lost with it.
    Well practice makes perfect...

    I'm trying to check is there row in the database with checking is there id..
    There is form with text field for $Nickname
    Code:
    PHP Code:
    $SQL "SELECT id FROM users WHERE nickname = '$Nickname'";
    if( 
    $STMT $DBH->exec$SQL ) == "1" ){
          echo 
    "OK";
    }else{
          echo 
    "NO";

    Even tough database has 'ECode' nickname field and I submit it;
    always returns with 'NO'.
    Thanks already for helping me

  • #2
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    The beauty of PDO is that you don't put raw variables into a query like that. PDO::exec will also not work with SELECT statements. Try this:
    PHP Code:
    $query $dbh->prepare("SELECT COUNT(id) FROM users WHERE nickname = ?");
    $query->execute($nickname);

    echo (
    $query->fetchColumn() == 1) ? "Ok" "No"
    Last edited by Nile; 08-11-2012 at 09:54 AM.

  • #3
    New Coder
    Join Date
    Aug 2012
    Location
    Finland
    Posts
    23
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Nile View Post
    The beauty of PDO is that you don't put raw variables into a query like that. PDO::exec will also not work with SELECT statements. Try this:
    PHP Code:
    $query $dbh->prepare("SELECT COUNT(id) FROM users WHERE nickname = ?");
    $query->execute($nickname);

    echo (
    $query->fetchColumn() == 1) ? "Ok" "No"
    I tried something like this too, but got error like now;
    Code:
    Warning: PDOStatement::execute() expects parameter 1 to be array, string given in [LINE]
    EDIT: It works now when I edited the code to this:
    PHP Code:
     $QUERY->execute(array( $Nickname )); 
    Thanks :P
    Last edited by ECoode; 08-11-2012 at 10:00 AM.

  • #4
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    *embarrassed*

    Change that line to:
    PHP Code:
    $query->execute(array($nickname)); 

  • Users who have thanked Nile for this post:

    ECoode (08-11-2012)


  •  

    Posting Permissions

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