首页 > 解决方案 > 带有连接的 MySQL 查询在 Phpmyadmin 中有效,但在 php 中无效

问题描述

我有一个 Mysql (mariaDB) 查询,其中一些连接在 phpmyadmin 终端中运行良好。但是,如果我尝试在 php (pdo) 中执行,数据库会得到巨大的工作量,直到我在nginx.

如果我从查询中删除连接,它也适用于我的 php 脚本。

所以问题一定出在连接上,但这里有什么问题?

public function exportCSVById($id){
$this->db->query('
SELECT DISTINCT
positions_list.id,
positions_list.order_id,
positions_list.position,
positions_list.comment,
positions_list.result,
positions_list.created,
positions_list.modified,
positions_list.releaseSuS1,
positions_list.releaseSuS2,
positions_list.releaseCuS1,
positions_list.releaseCuS2,
us1.lastname as SuS1,
us2.lastname as SuS2,
orderstates.keynr,
orderstates.content,
companies.id,
companies.company,
users.id,
users.lastname,
ordertype_positions.keynr,
ordertype_positions.p_company,
ordertype_positions.content as orderstate,
repkey.keynr,
repkey.p_company,
repkey.content as repkey,
rk2.keynr,
rk2.p_company,
rk2.content as repkey2

FROM positions_list

JOIN ordertype_positions
ON positions_list.optionindex=ordertype_positions.keynr
AND ordertype_positions.p_company = positions_list.comp_id

JOIN repkey
ON positions_list.keyindex=repkey.keynr
AND repkey.p_company = positions_list.comp_id

JOIN repkey as rk2
ON positions_list.keyindex2=rk2.keynr
AND rk2.p_company = positions_list.comp_id

JOIN companies
ON positions_list.comp_id=companies.id

JOIN users
ON positions_list.uid=users.id

JOIN users as us1
ON positions_list.releaseSuS1=us1.id

JOIN users as us2
ON positions_list.releaseSuS2=us2.id

JOIN orderstates
ON positions_list.repstate=orderstates.keynr

WHERE 
positions_list.order_id =:id

ORDER BY
positions_list.created ASC
');

$this->db->bind(':id', $id);

$results = $this->db->resultSet();
return $results;
}

public function resultSet(){
$this->execute();
    return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}

标签: phpmysqlpdo

解决方案


PHPMYADMIN 对结果进行了限制。

你只是有一个巨大的结果集。PHP 尝试将其全部加载到内存中:

return $this->stmt->fetchAll(PDO::FETCH_OBJ);

显然,这超出了您当前对 PHP 的内存限制。

解决方案?

分批工作,或开始使用游标:

什么是 PDO 可滚动光标?


推荐阅读