简体   繁体   English

使用MySQL Select的PHP块系统出现问题

[英]Trouble with PHP block system using mysql select

I am having a problem with a script. 我的脚本有问题。 First, it worked fine. 首先,它运行良好。 Then as the site progressed and I added more code to it the script stopped working. 然后,随着网站的进展,我向其中添加了更多代码,脚本停止工作。

<?php
//simple blocks: output activated block modules
if ($session->logged_in) {
    if ($session->isAdmin()) {
        $query = "SELECT `id`, `name`, `location`, `position`, `status` FROM `modules` WHERE position='left' and status='on' ORDER BY `id` ASC LIMIT 20";
        $result = mysql_query($query) or die(mysql_error());
    }
    else {
        $query = "SELECT `id`, `name`, `location`, `position`, `status`, `admin_req` FROM `modules`         WHERE position='left' and status='on' and admin_req='no' ORDER BY `id` ASC LIMIT 20";
        $result = mysql_query($query) or die(mysql_error());
    }
}

if (mysql_num_rows($result) == 0) {
    echo '<div class="borderBlock"><div class="textBlock"><center>'.$row['name'].'<br>'; 
    echo 'No blocks activated!';
    echo '</div></div><br></center>';
}

if ($page == "Administration") {
    while($row = mysql_fetch_assoc($result)) {
        echo '<div class="borderBlock"><div class="textBlock">    <center>'.$row['name'].'<br>'; 
        include('../'.$row['location'].'');
        echo '</div></div><br></center>';
    }
}
else{
    while($row = mysql_fetch_assoc($result)) {
        echo '<div class="borderBlock"><div class="textBlock">        <center>'.$row['name'].'<br>'; 
        include(''.$row['location'].'');
        echo '</div></div><br></center>';
    }
}
?>

Basically, what the script is supposed to do is pull data from a MySQL database and then parse it to produce a block, which calls certain modules. 基本上,脚本应该做的是从MySQL数据库中提取数据,然后解析它以生成一个块,该块调用某些模块。 If the user is a guest it will just populate the active modules that don't require administration. 如果用户是访客,它将仅填充不需要管理的活动模块。 If an admin is active it will populate the normal modules and also populate the administration modules, too. 如果管理员处于活动状态,它将填充普通模块,也将填充管理模块。

For some unknown reason this has stopped working and I can't seem to figure out why. 由于某种未知的原因,此操作已停止工作,我似乎无法弄清原因。

Its probably something so easy that I cannot see it and will kick myself later. 它可能很简单,我看不到它,以后会踢自己。

If this is bad practice I will consider suggestions for remodeling of the code. 如果这是不好的做法,我将考虑有关代码重构的建议。

Apologies I would also like to thank Francisco Presencia who also highlighted that the code I was using to be deprecated had I read the comment correctly I would have thanked you earlier. 致歉,我还要感谢Francisco Presencia,他还强调指出,如果我正确阅读了注释,那么我将不赞成使用该代码。 I would like to thank also Alex for explaining that there was no code to populate the modules for a guest and has Tom stated the error reporting tool showed me exactly what Francisco and Adrian stated. 我还要感谢Alex解释说,没有代码可以为访客填充模块,并且Tom指出错误报告工具向我准确地展示了Francisco和Adrian所说的内容。

<?php
//simple blocks: output activated block modules
if($session->logged_in){
if(!$session->isAdmin()){
global $database;
$stmt = $database->connection->query("SELECT `id`, `name`, `location`, `position`, `status`, `admin_req` FROM ".TBL_ACTIVE_MODULES." WHERE position='center' and status='on' and admin_req='no' ORDER BY `id` ASC LIMIT 20");
/* Error occurred, return given name by default */
$num_rows = $stmt->columnCount();

if(!$stmt || ($num_rows < 0)){
    echo '<center>'.$row['name'].'<br>'; 
    echo 'No blocks activated!';
    echo '<br></center>';
}
else if($num_rows > 0){
   /* Display active modules */
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        echo '<center>'.$row['name'].'<br>'; 
        include(''.$row['location'].'');
        echo '<br></center>';
    }
}
}
}

if($session->logged_in){
if($session->isAdmin()){
global $database;
$stmt = $database->connection->query("SELECT `id`, `name`, `location`, `position`, `status` FROM ".TBL_ACTIVE_MODULES." WHERE position='center' and status='on' ORDER BY `id` ASC LIMIT 20");
/* Error occurred, return given name by default */
$num_rows = $stmt->columnCount();

if(!$stmt || ($num_rows < 0)){
    echo '<center>'.$row['name'].'<br>'; 
    echo 'No blocks activated!';
    echo '<br></center>';
}
else if($num_rows > 0){
   /* Display active modules */
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        echo '<center>'.$row['name'].'<br>'; 
        include(''.$row['location'].'');
        echo '<br></center>';
    }
}
}
}

if(!$session->logged_in){
global $database;
$stmt = $database->connection->query("SELECT `id`, `name`, `location`, `position`, `status`, `logged_in` FROM ".TBL_ACTIVE_MODULES." WHERE position='center' and status='on' and logged_in='no' ORDER BY `id` ASC LIMIT 20");
/* Error occurred, return given name by default */
$num_rows = $stmt->columnCount();

if(!$stmt || ($num_rows < 0)){
echo '<center>'.$row['name'].'<br>'; 
echo 'No blocks activated!';
echo '<br></center>';
}
else if($num_rows > 0){
   /* Display active modules */
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        echo '<center>'.$row['name'].'<br>'; 
        include(''.$row['location'].'');
        echo '<br></center>';
    }
}
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM