访问while循环中的连接变量


accessing connection variable in a while loop

当我运行while循环时,我试图访问我的连接变量,但当我试图调用一个函数时,它会欺骗我,当我试图在函数中准备语句时,PHP会给我所谓的布尔错误。

我对它进行了调试,直到我知道我的变量$CategorieId正在被推送,并且当我在函数本身中执行print_r时,我确实得到了$con的数组返回。然而,当我在准备语句时尝试访问它时,它只会返回一个boolean,从而在下拉列表中创建错误,无法填充它。

设置如下。

dbControl.php

$con = mysqli_connect('localhost','root','','jellysite') or die("Error " . mysqli_error($con));  
function OpenConnection(){
    global $con;
    if (!$con){
        die('Er kan geen verbinding met de server of met de database worden gemaakt!');
    }
}

functionControl.php

function dropdownBlogtypeFilledin($con,$CategorieId){
    echo "<select name='categorie' class='dropdown form-control'>";     
    $sql = "SELECT categorieId, categorieNaam 
            FROM categorie"; 
    $stmt1 = mysqli_prepare($con, $sql);            
    mysqli_stmt_execute($stmt1);
    mysqli_stmt_bind_result($stmt1,$categorieId,$categorieNaam);          
    while (mysqli_stmt_fetch($stmt1)){
        echo "<option value='".$categorieId."'.";
        if( $categorieId == $CategorieId){
                echo "selected='selected'";
                }            
        echo ">".$categorieNaam."</option>";
    }
    echo "</select>";
}

Blogedit.php

<?php 
require_once '../db/dbControl.php';
require_once '../db/functionControl.php';
session_start();
OpenConnection();   
$id = $_SESSION['user'];    
?>
// some html up to the while loop
<?php
        $a = $_GET['a'];
        $sql = "SELECT blog.blogId,
                       blog.blogTitel,
                       blog.blogCategorieId,
                       blog.blogSynopsis,
                       blog.blogInhoud
                FROM   blog
                WHERE  blog.blogId = ? ";
        $stmt1 = mysqli_prepare($con, $sql);
        mysqli_stmt_bind_param($stmt1,'i',$a);
        mysqli_stmt_execute($stmt1);
        mysqli_stmt_bind_result($stmt1, $blogId, $Titel, $CategorieId, $Synopsis, $Inhoud );
        while (mysqli_stmt_fetch($stmt1)){      
            $Synopsis = str_replace(''r'n','', $Synopsis);
            $Inhoud = str_replace(''r'n','', $Inhoud);  
    ?>
    // again some HTML
     <?php dropdownBlogtypeFilledIn($con,$CategorieId); ?>
    // guess what, more html!
<?php   
    }
?>

有人知道我该怎么解决吗?我尝试使用全局变量(OpenConnection()函数),但似乎不起作用。

编辑

我确认它确实与$con变量有关。我通过在函数中再次定义$con变量来测试它,它完美地打印出了我想要的内容。这是一个糟糕的解决方案。我只是更喜欢定义一次。

奇怪的是,只有当我把它放在while循环中时,它才会发生。我有一个完全相同的创建表单,只是没有while循环,因为我是从头开始创建的,而且这部分没有涉及PHP。我还有一个下拉函数,它也需要$con变量,但在那里它可以工作。我真的认为这与while循环有关。

在启动检索信息的页面的准备语句之前,我通过创建连接变量的新实例暂时解决了这个问题。

 <?php  
        $connection = $con;  
        $a = $_GET['a'];
        $sql = "SELECT blog.blogId,
                       blog.blogTitel,
                       blog.blogCategorieId,
                       blog.blogSynopsis,
                       blog.blogInhoud
                FROM   blog
                WHERE  blog.blogId = ? ";
        mysqli_stmt_init($con);     
        $stmt1 = mysqli_prepare($con, $sql);
        mysqli_stmt_bind_param($stmt1,'i',$a);
        mysqli_stmt_execute($stmt1);
        mysqli_stmt_bind_result($stmt1, $blogId, $Titel, $CategorieId, $Synopsis, $Inhoud );
        mysqli_stmt_store_result($stmt1); 
        while (mysqli_stmt_fetch($stmt1)){      
            $Synopsis = str_replace(''r'n','', $Synopsis);
            $Inhoud = str_replace(''r'n','', $Inhoud);

       <?php dropdownBlogtypeFilledIn($connection ,$CategorieId); ?>
    ?>

使用变量$connection,我可以在while调用中启动需要连接详细信息的函数。我不确定这里是否有一个更干净的选项,但事实上,我在某个地方读到,如果我已经在准备好的语句中使用了相同的连接变量,我就不能使用它。看起来我不能用同样的连接变量来驾驭这个,这似乎就是问题所在。我将对此进行研究,并希望每当我有多个下拉列表时,我不必写一堆连接变量,例如从数据库中提取信息。