Boober Bunz Boober Bunz -5 years ago 244
MySQL Question

How to echo query result using PHP mysql (PDO)

This may seem silly, but I can't figure out how to do this simple thing in spite of much googling.

I have the fully functional PHP script that queries a mysql database to check for a boolean value. My script contains the code:

$sql = "SELECT truefalse FROM mydb WHERE bilbobaggins=:bilbobaggins";

$query = $db->prepare($sql);

try {


':bilbobaggins' => $bilbobaggins


} catch (Exception $e) {

echo "Query failed. " . ($e->getMessage());
$db = null;


All I'm trying to do is have the result of the query echo back from the script, such that for example, if I put

into a browser, it will return either a 1 or a 0 in text format.

I suspect I need to change the line:


To something like:

print $query->execute(array(

But that does not work.

Any help appreciated. Thanks.

Answer Source

I fixed it by adding the following:

while ($row = $query->fetch(PDO::FETCH_ASSOC))
echo $row['truefalse'];


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download