hi, i have some problem with my php code. when i run the php, the page keep loading and no output displayed. here is my code:

    $dayBeforeNew = $_GET['daybefore'];
    $dayAfterNew = $_GET['dayafter'];        
    $sql = "SELECT reference.LOCID, reference.Region, Sitename, (SELECT `Data Total Traffic` from 4g_statistic where reference.LOCID = 4g_statistic.LOCID AND 4g_statistic.Day = $dayBeforeNew) as `pre.Data Total Traffic`, (SELECT `L_25_VoLTE Traffic (Erlang)` from 4g_statistic where reference.LOCID = 4g_statistic.LOCID AND 4g_statistic.Day = $dayAfterNew) as `pre.L_25_VoLTE Traffic (Erlang)` FROM reference";
    $res = mysqli_query($conn, $sql) or die(mysqli_error($conn));
    while($row = mysqli_fetch_array($res))
    {
    ?>

        <tr>
          <td><?php echo $i; $i++; ?></td>      
          <td><?php echo $row['LOCID']; ?></td>
          <td><?php echo $row['Sitename']; ?></td>  
          <td><?php echo $row['Region']; ?></td>

          <td><?php echo $row['`pre.Data Total Traffic`']; ?></td>      
          <td><?php echo $row['`pre.L_25_VoLTE Traffic (Erlang)`']; ?></td>
        </tr>

        <?php 
    } 

i guess the problem is in the sql, because there are select in select statement. but i don't know how to solve it..

Recommended Answers

All 4 Replies

Then look at the contents of your variables $dayBeforeNew and $dayAfterNew

What data type is 4g_statistic.Day ? Is it date(time)? Yes, then you are missing single quotes.

afaik, in phpmyadmin queries are pasted in raw text, in php you're putting the query inside of double quotes " //query here ". But that does not mean that the variable embedded is considered string, unless you put single quotes ' //varname here '.

I just use curly brackets around the variables, and you don't need ` marks in the array reference later on. You also want to screen the GET input to make sure someone isn't going to drop your database. I also try not to use key words for table names, like reference. Maybe you need to say `reference` instead, but if it runs on the SQL server it will work in PHP.

    $dayBeforeNew = $_GET['daybefore'];
    $dayAfterNew = $_GET['dayafter'];

    if(!ctype_digit($dayBeforeNew)){$dayBeforeNew = 1;}//Prevent an SQL INJECTION NIGHTMARE
    if(!ctype_digit($dayAfterNew)){$dayAfterNew = 1;}//Prevent an SQL INJECTION NIGHTMARE
    $sql = "SELECT reference.LOCID, reference.Region, Sitename, (SELECT `Data Total Traffic` from 4g_statistic where reference.LOCID = 4g_statistic.LOCID AND 4g_statistic.Day = {$dayBeforeNew}) as `pre.Data Total Traffic`, (SELECT `L_25_VoLTE Traffic (Erlang)` from 4g_statistic where reference.LOCID = 4g_statistic.LOCID AND 4g_statistic.Day = {$dayAfterNew}) as `pre.L_25_VoLTE Traffic (Erlang)` FROM `reference`";
    $res = mysqli_query($conn, $sql) or die(mysqli_error($conn));
    while($row = mysqli_fetch_array($res))
    {
    ?>

        <tr>
          <td><?php echo $i; $i++; ?></td>      
          <td><?php echo $row['LOCID']; ?></td>
          <td><?php echo $row['Sitename']; ?></td>  
          <td><?php echo $row['Region']; ?></td>

          <td><?php echo $row['pre.Data Total Traffic']; ?></td>      
          <td><?php echo $row['pre.L_25_VoLTE Traffic (Erlang)']; ?></td>
        </tr>

        <?php 
    } 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.