Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 切换到PDO后未显示网页内容_Php_Mysql_Database_Pdo - Fatal编程技术网

Php 切换到PDO后未显示网页内容

Php 切换到PDO后未显示网页内容,php,mysql,database,pdo,Php,Mysql,Database,Pdo,我试着将我的代码从mysql切换到PDO,一切似乎都正常,因为我没有收到任何错误,但是网页的内容没有显示出来 这是我的数据库连接结构 try { $conn = new PDO('mysql:host=localhost;dbname=***', '***', '***', array(PDO::ATTR_PERSISTENT => true)); $conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PD

我试着将我的代码从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'));
    }?>
$conn是保存PDO连接实例的变量(对象)的名称


现在可能出现的问题是

您的代码所做的是嵌套查询。我看到两个嵌套查询。最后一个
$qry
是安全的


基本上,您正在覆盖
$query
结果集。例如,只需将另一个变量更改为
$query2
。为每个新准备的语句执行此操作,$conn可以重用。

使用PDO很好,但不要使用字符串连接来组合查询。这是危险的错误。您必须使用以避免出现严重问题。您的查询应该类似于
“从cat_id=:cat_id“
的部分中选择title,id,以及稍后使用
执行的调用(数组('cat_id'=>$output[“id”]))
@tadman是的,我会尽快开始修复。这可能是我的问题的原因吗?我刚刚检查了我的索引页的源代码。看看html表单中的错误,但它没有显示在页面上。这是怎么回事possible@tadman事实上,此代码有意抑制错误。因此,不需要CSS。@Pinkparter SQL注入一直是一个问题,但这里的PHP社区似乎基本上没有注意到这些问题。我通常不做PHP,但是这里发布的放射性错误代码的数量,就像这个简短的例子,完全失控了。知道自己在做什么的PHP开发人员
mysql\u real\u escape\u string
已经存在十多年了,在此之前还有其他方法。
 Notice: Undefined variable: conn in C:\wamp\www\xxx\functions\xxx.php on line 673