1

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. 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.

teynon
  • 6,248
  • 9
  • 56
  • 93
  • **mysql_*** is **DEPRECATED** use PDO or MySQLi! – Adrian Preuss Mar 22 '14 at 00:16
  • 1
    Add `error_reporting(E_ALL);` and `ini_set('display_errors', 1);` to the beginning of your file and then tell use what error message you are getting. If no error, what is it doing that it shouldn't be doing? – teynon Mar 22 '14 at 00:17
  • 1
    I'm surprised. First I see some OOP programming (`$session->block`) and then I see the ld, deprecated mysql_*. I never thought I'd see something like that. Besides, I'd recommend separating PHP from HTML. The code gets really messy when combined in that way. – Francisco Presencia Mar 22 '14 at 00:26
  • It looks like no query is made to MySQL at all if `$session->logged_in` doesn't evaluate to true (i.e. the user isn't logged in). Otherwise, without knowing what code you added to make it stop working, it's hard to say what's caused your problem. – Alex Mar 22 '14 at 01:01
  • Apologies the code i added to was the actual SQL query i added more fields to it so that it looked at more options. – Craig Booth Mar 24 '14 at 18:47
  • Potentially that could be the issue that it is missing the SQL query if the user is not logged in but that doesnt explain why the administration part is not working. – Craig Booth Mar 24 '14 at 18:49

1 Answers1

0

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. 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.

<?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>';
    }
}
}

?>