PDO:使$dbh可用,并在所有php文件中进行维护

PDO: make $dbh available and maintain across all php files

本文关键字:文件 php 维护 dbh 可用 PDO      更新时间:2023-09-26

我似乎做不好。基本上,使用了三个PHP文件:-login.PHP、testconnect.PHP和numrows.PHPnumrows.php是第一次开始播放的主文件。

login.php和testconnect.php都很好。

numrows.php:-

<?php
global $dbh1;
require_once "testconnect.php";
try
    {  
    $stmt = $dbh1->prepare("select count(distinct mfg_code) from test");
    $stmt->execute();
}
catch(PDOException $err)
{
    $alertmsg = $err->getMessage();
    }
$num = $stmt->fetch("PDO::FETCH_ASSOC:");
$num = json_encode($num);
echo $num;
?>

来自apache的日志错误显示"GET/testnumcards.php HTTP/1.1"500-"。同样,我在调试时遇到的错误是"NetworkError:500内部服务器错误"。正确的做法是什么?

您的问题不是使$dbh可用,而是代码不一致和语法错误
至少这样做你的文件,没有所有无用和错误的代码

<?php
require_once "testconnect.php";
$stmt = $dbh1->prepare("select count(distinct mfg_code) from test");
$stmt->execute();
$num = $stmt->fetch(PDO::FETCH_ASSOC); // note the proper syntax
$num = json_encode($num);
echo $num;

然后查看error_log以获取详细信息