/    Sign up×
Community /Pin to ProfileBookmark

PDO Conditional Select Statement Failing

My fourth day using PHP has proven to be the hardest. I have a MySQL db set up that stores members login credntials. On the Login.php page I have the following code:

[code=php]
try {
$stmt = $db->prepare(“SELECT * FROM members WHERE username = :uname AND password = :pwd1”);
$stmt->bindParam(‘:uname’, $uname, PDO::PARAM_STR);
$stmt->bindParam(‘:pwd1’, $pwd1, PDO::PARAM_STR,40);
$stmt->execute();

$user_id = $stmt->fetchColumn();

if($user_id == false) {
echo ‘Login Failed’;
}

else {
echo ‘You are now logged in’;
}
}

catch (PDOException $ex) {
echo ‘We are unable to process your request. Please try again later’;
}
[/code]

$uname and $pwd1 are from the form, and what I’m entering is indeed in the db. But I never get any echoes telling me success or failure. All I get when I submit the login info is a blank page. Will someone tell me what I’m doing wrong? Just need a push in the right direction is all.

Many thanks,
Landslyde

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@Sup3rkirbyJan 29.2015 — This wouldn't be the cause of your problem (or I wouldn't think so), but I generally avoid the bindParam() method as it can get messy sometimes. Not that it's bad, but you can also pass in bound parameters into the execute() function, like so:
[code=php]
$stmt = $db->prepare("SELECT * FROM members WHERE username = :uname AND password = :pwd1");
$stmt->execute(array("uname"=>$uname, "pwd1"=>$pwd1));
[/code]


Anyway, the first thing I would do is after you execute() the query, try adding this line:
[code=php]
echo "Rows: " . $stmt->rowCount();
[/code]


I'm mainly just curious as to if any rows are being found as a result of the query.

Though something makes me wonder if maybe there is a PHP error earlier in the code (outside of your try/catch). If that were try, the script would stop and never echo anything and you'd get your white/blank page.
Copy linkTweet thisAlerts:
@LandslydeauthorJan 29.2015 — This wouldn't be the cause of your problem (or I wouldn't think so), but I generally avoid the bindParam() method as it can get messy sometimes. Not that it's bad, but you can also pass in bound parameters into the execute() function, like so:
[code=php]
$stmt = $db->prepare("SELECT * FROM members WHERE username = :uname AND password = :pwd1");
$stmt->execute(array("uname"=>$uname, "pwd1"=>$pwd1));
[/code]


Anyway, the first thing I would do is after you execute() the query, try adding this line:
[code=php]
echo "Rows: " . $stmt->rowCount();
[/code]


I'm mainly just curious as to if any rows are being found as a result of the query.

Though something makes me wonder if maybe there is a PHP error earlier in the code (outside of your try/catch). If that were try, the script would stop and never echo anything and you'd get your white/blank page.[/QUOTE]


Thanks for repsonding, Sup3rkirby;

I changed my $stmt->execute to the one you supplied. I also set the echo following that line. Nothing. No errors, no echoes. I use

[code=php]
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
[/code]


when connecting to my db, so I think I'm okay there. I also have my php.ini set to: error_reporting = E_ALL and display_errors = On

So since it doesn't throw an error and doesn't show an echo, I'm at a total loss. Any ideas?

~Landslyde
Copy linkTweet thisAlerts:
@LandslydeauthorJan 29.2015 — Had a Fatal Error. Found it in the server log and fixed the problem. Still not sure why it didnt throw an error in the script though.

~Landslyde
Copy linkTweet thisAlerts:
@NogDogJan 29.2015 — Had a Fatal Error. Found it in the server log and fixed the problem. Still not sure why it didnt throw an error in the script though.

~Landslyde[/QUOTE]


May have to turn on display_errors in the script -- though if it's a parse error of some sort, it will only display if it's turned on in your PHP config.
[code=php]
<?php
error_reporting(E_ALL);
ini_set('display_errors', true); // set to false for production
[/code]
Copy linkTweet thisAlerts:
@LandslydeauthorJan 29.2015 — I fixed the problem, NogDog. In thinking my error enabling was turned on, it turns out that I was looking at the wrong php.ini for that. The one being used on my Linux box was in /etc/php/apache2/. Made the changes and the world's a better place for it ?

~Landslyde
×

Success!

Help @Landslyde spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...