我试图将我的代码从 mysql 切换到 PDO 一切似乎都在工作,因为我没有收到任何错误,但网页的内容没有显示。
这是我的数据库连接结构
try {
$conn = new PDO('mysql:host=localhost;dbname=***', '***', '***', array(PDO::ATTR_PERSISTENT => true));
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Problem with connection ".$e->getMessage();
}
所以我尝试了一个新页面,其中包含我使用 PDO 在我的库中拥有的功能之一,它显示了
<?php
require_once('functions/generalfunctions.php');
try {
$conn = new PDO('mysql:host=localhost;dbname=***', '***', '***');
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "success";
} catch(PDOException $e) {
echo "I'm sorry there is a problem with your operation..";
file_put_contents( 'dbErrors.txt', $e->getMessage(), FILE_APPEND );
}
try{
$query = $conn->prepare("SELECT * FROM categories ORDER BY title");
$query->execute();
$i = 0;
while($output = $query->fetch()){
echo '<li><a href="category.php?cat_id='.encrypt_id($output["id"]).'" class="parent" rel="'.$i.'">'.$output["title"].'</a>'. "\n" .'<ul class="child'.$i.'">';
$stm = "SELECT title,id FROM sections WHERE cat_id=".$output["id"]." ORDER by title";
$query = $conn->prepare($stm);
$query->execute();
while($out = $query->fetch()){
$stm = "SELECT count(sec_id) AS topic_count FROM topic WHERE sec_id =".$out["id"];
$qry = $conn->prepare($stm);
$qry->execute();
$cnt = $qry->fetch();
echo "<li>";
echo '<a href="sections.php?sec_id='.encrypt_id($out["id"]).'&cat_id='.encrypt_id($output["id"]).'">'.$out["title"]."</a><span id=\"cnt_no\" class=\"badge badge-inverse\">".$cnt["topic_count"];
echo "</span></li>";
}
echo "</ul>\n</li>";
$i++;
}
}catch(Exception $e){
die(header('location: http://localhost/test'));
}?>
所以我不知道问题出在哪里。请帮忙
我刚刚发现了一个主要问题……它说
Notice: Undefined variable: conn in C:\wamp\www\xxx\functions\xxx.php on line 673
$conn 是保存 PDO 连接实例的变量(对象)的名称。
现在可能是什么问题