Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-11-2012, 08:12 AM   PM User | #1
ECoode
New Coder

 
Join Date: Aug 2012
Location: Finland
Posts: 23
Thanks: 2
Thanked 5 Times in 5 Posts
ECoode is an unknown quantity at this point
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
ECoode is offline   Reply With Quote
Old 08-11-2012, 08:51 AM   PM User | #2
Nile
Regular Coder

 
Nile's Avatar
 
Join Date: Jun 2008
Posts: 280
Thanks: 2
Thanked 46 Times in 46 Posts
Nile is an unknown quantity at this point
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 08:54 AM..
Nile is offline   Reply With Quote
Old 08-11-2012, 08:57 AM   PM User | #3
ECoode
New Coder

 
Join Date: Aug 2012
Location: Finland
Posts: 23
Thanks: 2
Thanked 5 Times in 5 Posts
ECoode is an unknown quantity at this point
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 09:00 AM..
ECoode is offline   Reply With Quote
Old 08-11-2012, 09:00 AM   PM User | #4
Nile
Regular Coder

 
Nile's Avatar
 
Join Date: Jun 2008
Posts: 280
Thanks: 2
Thanked 46 Times in 46 Posts
Nile is an unknown quantity at this point
*embarrassed*

Change that line to:
PHP Code:
$query->execute(array($nickname)); 
Nile is offline   Reply With Quote
Users who have thanked Nile for this post:
ECoode (08-11-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:14 PM.


Advertisement
Log in to turn off these ads.