我正在从mysql迁移到PDO。我正在创建一个函数,但在调用get error时:
Catchable fatal error: Object of class stdClass could not be converted to string in file * on line **当我有"e.match_id = '$row1->match_id'“的东西时,我不确定如何处理这种类型的迁移。使用旧mysql的代码:
$query11 = "SELECT COUNT(e.event_id) as numgoals
FROM event e
WHERE (e.match_id = '$row1->match_id' AND e.team_id = '$row1->team_a_id' AND e.eventtype IN ('soc_G','soc_PG') ) OR
(e.match_id = '$row1->match_id' AND e.team_id = '$row1->team_b_id' AND e.eventtype = 'soc_OG' )";
$result11 = mysql_query($querye11);
$row11 = mysql_fetch_object($result11);使用PDO的功能:
public function getResult11($row)
{
$query = "SELECT COUNT(e.event_id) as numgoals
FROM event e
WHERE (e.match_id = '?->match_id' AND e.team_id = '?->team_a_id' AND e.eventtype IN ('soc_G','soc_PG') ) OR
(e.match_id = '?->match_id' AND e.team_id = '?->team_b_id' AND e.eventtype = 'soc_OG' )";
$statement = $this->db->prepare($query);
$statement->execute(array($row));
return $statement->fetchObject();
}如何通过函数参数将"e.match_id = '$row1->match_id'“转换为"$row1”?我使用了这个"e.match_id = '?->match_id'",但不确定它是否正确。
发布于 2016-04-14 18:18:18
您在字符串中错误地使用了变量:
SELECT $match->match_id将仅转换$math,并保持->match_id不变。
在变量周围使用{}:
SELECT {$match->match_id}预准备语句的正确用法
$query = "[...] WHERE (e.match_id = :mId AND e.team_id = :t1Id AND e.eventtype IN ('soc_G','soc_PG')) OR
(e.match_id = :mId AND e.team_id = :t2Id AND e.eventtype = 'soc_OG')";
$statement = $this->db->prepare($query);
$statement->execute([
':mId' => $row1->team_a_id,
':t1Id' => $row1->team_a_id,
':t2Id' => $row1->team_b_id,
]);在循环中使用时为
$statement = $this->db->prepare("SELECT * FROM table WHERE id = :id");
foreach ($ids as $id) {
$statement->execute([
':id' => $id,
]);
}https://stackoverflow.com/questions/36620076
复制相似问题