Php 简单的PDO查询可以在dev服务器上工作,但不能在生产环境中工作。为什么?
我有下面的PDO查询,它在开发服务器上运行良好,但在实时服务器上,返回一个空的结果集 两台服务器上都存在相同的数据库副本,httpd或mysql日志中都没有错误消息。也没有任何PDO例外Php 简单的PDO查询可以在dev服务器上工作,但不能在生产环境中工作。为什么?,php,pdo,Php,Pdo,我有下面的PDO查询,它在开发服务器上运行良好,但在实时服务器上,返回一个空的结果集 两台服务器上都存在相同的数据库副本,httpd或mysql日志中都没有错误消息。也没有任何PDO例外 <? class DB { public function __construct() { global $dbh; try { $dbh = new PDO('mysql:host=localhost;dbname=snomweb_main', 'snomdb_u
<?
class DB {
public function __construct() {
global $dbh;
try {
$dbh = new PDO('mysql:host=localhost;dbname=snomweb_main', 'snomdb_user', '6pjOjYpcRpEZFdsu');
$dbh ->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
public function getFAQCats2Array() {
global $dbh;
try {
$q = '
SELECT
`id` AS ci,
`name` AS n
FROM
`faqcat`;
';
$s = $dbh->query($q);
// initialise an array for the results
$A = array();
if ($s->execute()) {
while ($r = $s->fetch(PDO::FETCH_ASSOC)) {
$A[] = $r;
}
}
$s = null;
return $A;
}
catch(PDOException $e) {
echo "Something went wrong fetching the list of FAQ categories from the database.\n";
file_put_contents(
$_SERVER['DOCUMENT_ROOT']."/PDOErrors.txt",
"\n\n\n\n".$e->__toString(), FILE_APPEND);
}
}
}
当您执行$pdo->query($sql_语句)
时,返回值是一个有效的PDOStatement
,您可以直接迭代以获取结果,而无需执行$s->execute()
语句
所以只需删除:)您有一个类,并且类中有成员,为什么您使用global
我认为这不会纠正您的问题,但是您不必对$s->execute()
查询()的结果调用$pdo->query()
,因为查询没有WHERE
子句,除了live server中的表为空之外,我想不出任何原因。@CarlosCampderrós我完全删除了if
语句,解决了问题!也许你可以给我提个问题,这样我就可以接受了。谢谢。另一个简化:您的fetch
循环相当于调用fetchAll()
。
<?
require_once '../inc/classes/db.class.php';
$DB = new DB();
$cArr = $DB->getFAQCats2Array();
$qArr = $DB->getFAQQuestions2Array();
foreach ($cArr AS $c) { // do this for each group ?>
<h3><?=$c['n']?></h3>
<div>
<? foreach ($qArr AS $q) { // do this for each question in group
if ($q['ci'] === $c['ci']) {
?>
<a href="faq-answers.php?i=<?=$q['qi']?>" title=""><?=$q['q']?></a><br>
<?
} // end 'if'
} // /do this for each question in group
?>
</div>
<? } // /do this for each group ?>