gpt4 book ai didi

php - echo PDO 查询结果,返回空?

转载 作者:行者123 更新时间:2023-11-29 12:31:40 25 4
gpt4 key购买 nike

我正在学习 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>";

最佳答案

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

关于php - echo PDO 查询结果,返回空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27395889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com