Php 使用$sth获取一个字段->;执行()

Php 使用$sth获取一个字段->;执行(),php,pdo,Php,Pdo,我有以下代码: $sql = "SELECT personid FROM accountpersonmap WHERE accountid = :accountid; "; $array = array( 'accountid' => $eachRow['accountid'] ); $sth = $dbh->prepare($sql); $sth->execute(array(':personid' => $personid)) echo $pers

我有以下代码:

 $sql = "SELECT personid FROM accountpersonmap WHERE accountid = :accountid; ";
 $array = array(
    'accountid' => $eachRow['accountid']
 );
 $sth = $dbh->prepare($sql);
 $sth->execute(array(':personid' => $personid))
 echo $personid;
我只想要那个人,好吗? 我在谷歌上搜索,但没有找到我要找的东西


谢谢你,你不需要谷歌,只需要教程。这里有一个:

要获取值,必须获取它

另外,根据这个变量,
$eachRow['accountid']
,您似乎需要在其他查询中使用JOIN,而不是在循环中运行这个查询

更新

如上所述,您需要一个教程
这不仅仅是一个cargo cult示例,也是一个学习和理解如何使用PDO的教程。例如,如何从PDO获取错误。仔细看,您的代码中有一个愚蠢的输入错误:

 WHERE accountid = :accountid;
                   ^^^^^^^^^^^
但是


如果配置正确,PDO应该报告它。

类似的方法应该可以工作

$sql = "SELECT personid FROM accountpersonmap WHERE accountid = :accountid ";
$sth = $dbh->prepare($sql);
$sth->execute(array(':accountid' => $eachRow['accountid']));
$personid = $sth->fetchColumn();
echo $personid;

我尝试了这个,但没有得到任何结果:)编辑的代码没有返回任何结果?谢谢,伙计!:)现在工作,我做错了,我的坏,对不起,我有一个外部循环,但不需要加入任何东西。$eachRow['accountid']包含我想要的accountid,但是现在需要获取属于accountid的personid:)Yes。这就是JOIN的作用。这就是我的循环:foreach($paramData['arrayparams']as$key=>$eachRow){///mycode}和数组参数:'arrayparams'=>array(array('accountid'=>128887,'title'=>'你可以叫我miss update'、'firstname'=>'firstname update'、'lastname'=>'lastname update'}只要重写它加入并摆脱这个嵌套的查询检查这个
打印($sth)
并告诉我们结果我尝试了fetch_列,并尝试了其他人编写的代码中的示例,但没有用print_r($sth)来定义我要查找的内容:[queryString]=>从accountpersonmap中选择personid,其中accountid=:accountid;
$sql = "SELECT personid FROM accountpersonmap WHERE accountid = :accountid ";
$sth = $dbh->prepare($sql);
$sth->execute(array(':accountid' => $eachRow['accountid']));
$personid = $sth->fetchColumn();
echo $personid;