从PDO查询回显结果,返回空?

时间:2014-12-10 07:58:27

标签: php mysql pdo

我正在学习PDO,所以对我很温柔! 我试图回应我用PDO做的查询,但字符串返回空。 我在这里缺少什么?

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您需要此查询:

SELECT * FROM yourTable WHERE [...你的参数...] LIMIT 1