![](/img/trans.png)
[英]How do I make execute with PDO output a string rather than a single-element array?
[英]Using PDO to return a single set of values rather than an array
这个网站上的一个非常好的人帮我编写了以下脚本(它很好用)
<?php
$db = new PDO('mysql:host=HOST;dbname=DATABASE', $user, $pass);
$stmt = $db->prepare('
SELECT
yeast,
rating,
description,
weblink,
image,
sideimage
FROM dowdb_yeast_selector
WHERE
fruit = :fruit
ORDER BY
rating DESC
');
$stmt->bindParam(':fruit',$_POST['fruit'],PDO::PARAM_STR,50);
$stmt->execute();
如何仅回显侧面图像(不作为while循环的一部分)?
我想(?)我需要类似echo '$row[sideimage]'
//我有多简单
到目前为止,我看过的所有示例都不符合我的需求:-(
例如,使用PDO :: fetchAll;
$stmt->execute();
$arrResults = $stmt->fetchAll();
//$arrResults will be multidimensional
//This will echo the first sideimage
echo $arrResults[0]['sideimage'];
如果要回sideimage
所有值(即所有行),则必须遍历结果;
foreach($arrResults as $arrRow) {
echo $arrRow['sideimage'] . PHP_EOL;
}
您应该使用循环pdo :: fetch()
while($abc = $stmt->fetch())
{
print_r($abc);
}
如果您不想使用循环,请尝试pdo :: fetchAll()
$data = $stm->fetchAll();
您可以使用FETCH_ASSOC
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo $res[0]['sideimage'];
要么
foreach($res as $key=>$value) {
$image_val = $value['sideimage'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.