帮助将此 mysql 查询变成 PDO 查询
我已经使用它有一段时间了,但从那时起我升级到 PDO 而不是 mysql 查询。但我似乎根本无法让这个特定的片段发挥作用。
$query = mysql_query ($sql) or die (mysql_error());
while ($records = @mysql_fetch_array ($query)) {
$alpha[$records['alpha']] += 1;
${$records['alpha']}[$records['id_clients']] = array(
$records['first_name'], //item[0]
);
}
我已经尝试过了
while ($records = $this->db->fetch_row_assoc($sql)) {
$alpha[$records['alpha']] += 1;
${$records['alpha']}[$records['id_clients']] = array(
$records['first_name'], //item[0]
);
}
,我的 PDO 代码是
public function query($statement)
{
return self::$PDO->query($statement);
}
public function fetch_row_assoc($statement) {
self::$PDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
try
{
$stmt = self::$PDO->query($statement);
$result = $stmt->fetch();
return $result;
}catch(PDOException $e){
echo $e->getMessage();
}
return false;
//return self::$PDO->query($statement)->fetch(PDO::FETCH_ASSOC);
}
查询函数有效,但我引入了 fetch_row_assoc 函数,希望我可以使用 mysql_fetch_asso 做同样的事情。
$sql = "SELECT
SUBSTRING(`last_name`, 1, 1) AS alpha,
SUBSTRING(`middle_name`, 1, 1) AS subMiddleName,
`id_clients`,
`type`,
`first_name`,
`middle_name`,
`last_name`,
`address`,
`primary_number`,
`secondary_number`,
`home_number`,
`office_number`,
`cell_number`,
`fax_number`,
`ext_number`,
`other_number`,
`comments`
FROM `clients`
WHERE `user_id` = 1
AND `is_sub` = 0
AND `prospect` = 1
ORDER BY `last_name`";
I have been using this for a while, but have since then upgraded to PDO instead of mysql queries. But I can't seem to get this particular snippet to work at all.
$query = mysql_query ($sql) or die (mysql_error());
while ($records = @mysql_fetch_array ($query)) {
$alpha[$records['alpha']] += 1;
${$records['alpha']}[$records['id_clients']] = array(
$records['first_name'], //item[0]
);
}
I have tried this
while ($records = $this->db->fetch_row_assoc($sql)) {
$alpha[$records['alpha']] += 1;
${$records['alpha']}[$records['id_clients']] = array(
$records['first_name'], //item[0]
);
}
and my PDO code is
public function query($statement)
{
return self::$PDO->query($statement);
}
public function fetch_row_assoc($statement) {
self::$PDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
try
{
$stmt = self::$PDO->query($statement);
$result = $stmt->fetch();
return $result;
}catch(PDOException $e){
echo $e->getMessage();
}
return false;
//return self::$PDO->query($statement)->fetch(PDO::FETCH_ASSOC);
}
The query function works, but I have introduced my fetch_row_assoc function in hopes that I can do that same thing with mysql_fetch_asso.
$sql = "SELECT
SUBSTRING(`last_name`, 1, 1) AS alpha,
SUBSTRING(`middle_name`, 1, 1) AS subMiddleName,
`id_clients`,
`type`,
`first_name`,
`middle_name`,
`last_name`,
`address`,
`primary_number`,
`secondary_number`,
`home_number`,
`office_number`,
`cell_number`,
`fax_number`,
`ext_number`,
`other_number`,
`comments`
FROM `clients`
WHERE `user_id` = 1
AND `is_sub` = 0
AND `prospect` = 1
ORDER BY `last_name`";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: