skip to Main Content

Ok so I’m trying to make a localhost site that has the same base functions as Phpmyadmin, and everything is working other than displaying a table’s data.

here’s an example of what I’m trying to accomplish:
enter image description here

though I’m not sure how to accomplish this. Here is some code to show you what I have now

<div class="content">
    <?php $query2 = "SELECT * FROM " . $table; ?>
    <div class="query-class">
        <?php echo $query2; ?>
    </div>
    <h1>
        Tables In <?php echo $db; ?>
    </h1>
    <table>


        <?php
            $columquery = "SHOW COLUMNS FROM " . $table;
            $columresult = mysql_query($columquery);
            while ($row3 = mysql_fetch_array($columresult)) {
                echo "<th>" . $row3['Field'] . "</th>";
            }
        ?>

        <?php

            $result2 = mysql_query($query2);
            while ($row2 = mysql_fetch_array($result2)) {
                foreach($row2 as $var) {
                    echo "<tr><td>" . $var . "</td></tr>";   
                }

            }
        ?>
        </table>
</div>

Yes yes, I know it’s horrible.

4

Answers


  1. I think you are looking for something very ugly like the following. I found it in the garbage. I am not responsable for any use of it:

    <?php
    $db=Db::getConnection(); // singleton
    $this->var['result']=$db->query($_POST['query']);
    
    if (isset($this->var['result'])) {
        echo '<table cellpadding="5" cellspacing="2" border="1"><tbody>';
        $titles=array_keys(@$this->var['result'][0]);
        echo '<tr>';
        foreach($titles as $title)
            echo '<th>'.$title.'</th>';
        echo '</tr>';
    
        foreach($this->var['result'] as $row) {
            echo '<tr>';
            foreach($row as $cell) {
                echo '<td>'.$cell.'</td>';
            }
            echo '</tr>';
        }
    
        echo '</tbody></table>';
    }
    ?>
    

    Db is an standard singleton that contains a mysqli private object.
    query() contains something like

    $sql = $this->mysqli->query($query);
    $this->lastInsertId = $this->mysqli->insert_id;
    $errno = $this->mysqli->connect_errno;
    if ($errno === 0) {
        if ($this->mysqli->field_count > 0) {
            while ($row = $sql->fetch_assoc()) $response[] = $row;
            return $response;
        }else{
            return true;
        }
    }
    

    to create the “array of arrays” response. Suitable for the output you are looking for.

    Login or Signup to reply.
  2. If i understood you well, You need mysqli_fetch_row.

    $q= "SELECT * FROM  table";
    $result = $mysqli->query($q)
        while ($row = $result->fetch_row()) {
            print  ("$row[0], $row[1]);
        }
    
    Login or Signup to reply.
  3. I’ve added some escaping and only queried the database when needed:

    // Print table tags
    echo "<table>";
    
    // Print out records 
    $q = mysql_query("SELECT * FROM {$table};");
    if($res = $result->fetch_all(MYSQLI_ASSOC)){
        // Print out columns 
        $columns = array_keys($res[0]);
        echo'<tr><th>'.implode('</th><th>', array_map('htmlspecialchars',$columns).'</th></tr>';
        // Print out table data
        echo'<tr><td>'.implode('</td><td>', array_map('htmlspecialchars',$res).'</td></tr>';
    } else {
        // IFF there is no data, print out columns
        $q = mysql_query("SHOW COLUMNS FROM {$table};");
        if($res = $result->fetch_all(MYSQLI_ASSOC)){
            // Print columns
            echo'<tr><th>'.implode('</th><th>', array_map('htmlspecialchars',$res).'</th></tr>';
        }
    }
    echo '</table>';
    

    Hope this helps,

    Login or Signup to reply.
  4. The other answers use the mysqli API while you’re using the older, no longer supported mysql API. I really recommend upgrading to either mysqli or PDO, but if you want to stay with mysql you can use the following solution:

    <div class="content">
        <?php $query2 = "SELECT * FROM " . $table; ?>
        <div class="query-class">
            <?php echo $query2; ?>
        </div>
        <h1>
            Tables In <?php echo $db; ?>
        </h1>
        <table>
            <?php
                $shouldOutputHeaders = true;
                $result2 = mysql_query($query2);
    
                while ($row2 = mysql_fetch_assoc($result2)) {
                    if ($shouldOutputHeaders) {
                        echo "<tr>";
                        foreach (array_keys($row2) as $header) {
                            echo "<th>" . $header . "</th>"; 
                        }
                        echo "</tr>";
    
                        $shouldOutputHeaders = false;
                    }
    
                    echo "<tr>";
                    foreach ($row2 as $var) {
                        echo "<td>" . $var . "</td>"; 
                    }
                    echo "</tr>";
                }
            ?>
        </table>
    </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search