StoneStreet
StoneStreet

Reputation: 39

echo results from PDO query, returns empty?

I'm learning PDO so be gentle with me! I'm trying to echo out my query made with PDO, but the string returns empty. What am i missing here?

PHP

$query = "SELECT 1 
          FROM table 
          WHERE c1 = :c1 && c2 = :c2"; 

$query_params = array( ':c1' => $c1, ':c2' => $c2 ); 
try{ 
    $stmt = $db->prepare($query); 
    $result = $stmt->execute($query_params); 
} 
catch(PDOException $ex){
    die("Failed to run query: " . $ex->getMessage());
}
$row = $stmt->fetch(); //Now $row should hold values of c1 & c2, right?

//This is What i've tried
echo "<script type='text/javascript'>alert('".$row."');</script>";
echo "<script type='text/javascript'>alert('".$row[0]."');</script>";
echo "<script type='text/javascript'>alert('".$row[1]."');</script>";
echo "<script type='text/javascript'>alert('".$row['c1']."');</script>";
echo "<script type='text/javascript'>alert('".$result."');</script>";

Upvotes: 0

Views: 100

Answers (2)

shaolin
shaolin

Reputation: 473

you need this query:

SELECT * FROM yourTable WHERE [...your params..] LIMIT 1

Upvotes: 0

Konstantin
Konstantin

Reputation: 3450

If you need one row use SELECT * FROM table WHERE c1 = :c1 && c2 = :c2 LIMIT 1

Upvotes: 1

Related Questions