For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > October 2007 > Re: [PHP-DB] parameters not working under PDO









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] parameters not working under PDO
Thodoris

2007-10-31, 7:00 pm



O/H Chris Curvey ??????:
> It's quite possible that I'm missing something obvious here. The
> following code fragment does not return any rows, but if I take out
> the parameters and replace them with hardcoded strings (enclosed in
> single quotes), I get the right results.
>
> I've scattered "print" statements throughout, and the query seems to
> get past execute() OK, it's just not returning anything from the call
> to fetch()
>
> Am I missing something obvious?
>
> $stmt = $conn->prepare("select t.z from towns t
> join counties c on t.county_z = c.z
> where t.me = ?
> and c.state_z = ?");
> if ($stmt->execute($parts)) {
> while ($row = $stmt->fetch()) {
> $town_z = $row['z'];
> }

What is $parts doing ?? You just need execute() and I suppose although
not posted that you did construct the object like:
$conn| = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

|A good choice is to tell fetch a way to retrieve your data like this:
$town_z = $stmt->fetch(PDO::FETCH_ASSOC);
although I think that it will get both result sets in case you don't define.
> } else {
> print $stmt->errorCode();
> print_r($stmt->errorInfo());
> }
>
> Thanks in advance!
>

A better way debug that I know is using exceptions like:
try {
$sth = $conn->query($query);
$rs = $sth->fetch();
} catch (Exception $e) {
print "failed :".$e->getMessage();
}

You can use try {} with almost everything so give it a "try" :-) .

Send us some feed back or post full source if you keep having trouble.

--
Thodoris


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com