简体   繁体   中英

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>";

如果需要一行,请使用SELECT * FROM table WHERE c1 = :c1 && c2 = :c2 LIMIT 1

you need this query:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM