Question on print data on MySQL table

Discussion in 'Server Operation' started by satimis, May 29, 2009.

  1. satimis

    satimis New Member

    Hi folks,

    I have a php script;

    # cat /home/satimis/sqlusers.php
    Code:
    <?php
    
    $host="localhost"; //Database host.
    $user="root"; //Database username.
    $pass="apassword"; //Database password.
    $dbase="maildb"; //Database.
    
    $connect=mysql_connect($host,$user,$pass); //Connect to the database.
    mysql_select_db($dbase, $connect);
    $Q = "SELECT * FROM users"; // select everything
    $result = mysql_query($Q);
    while ($row = mysql_fetch_assoc ($result) ) { // loop through the result set
        echo $row["id"] . "<br>";
        echo $row["name"] . "<br>";
        echo $row["crypt"] . "<br>";
    }
    
    ?>
    On running;

    # php /home/satimis/sqlusers.php
    Code:
    [email protected]<br>satimis<br>0e3QZ140ZrOOE<br>[email protected]<br>smsliu<br>0eWBW8//TnfEg<br>[email protected]<br>albert<br>XNP5IEvi8VZS6<br>[email protected]<br>patricia<br>0eg78AAkIGLWM<br>
    [email protected]<br>postmaster<br>0e130m.vyL/aU<br>[email protected]<br>Albert Cheung<br>XXvTmrqgLrBGY<br>[email protected]:/#
    
    it printouts the data of mysql_table "users" but not in table form.

    I tried putting the script on /var/www/ and renamed it as "index.html" On browser to evoke the file it did not help. No data was displayed.

    Please help how to display the data on a mysql_table in table form. TIA

    B.R.
    satimis
     
  2. falko

    falko Super Moderator ISPConfig Developer

    Rename it to index.php instead and try again.
     
  3. satimis

    satimis New Member

    Solved

    Hi falko,

    My previous script doesn't work. Following script works nicely;

    # cat /var/www/sqlusers.php
    Code:
    <?php
    
    //These variables will determine the search parameters later
    
    $host="localhost"; //Database host.
    $user="root"; //Database username.
    $pass="apassword"; //Database password.
    $dbase="maildb"; //Database.
    
    $connect=mysql_connect($host,$user,$pass); //Connect to the database.
    mysql_select_db($dbase, $connect);
    
    //after the connection is made use the INSERT command to enter the values in the db
    $Q = "SELECT * FROM users"; // select everything
    
    //result set
    $result = mysql_query($Q);
    
    //creating the table /w headers
    echo "<html><body>";
    echo "<table><tr><td>id</td><td>name</td><td>crypt</td></tr>";
    
    //row for each record
    while ($row = mysql_fetch_assoc ($result) ) { // loop through the result set
    echo"<tr><td>" . $row['id'] . "</td><td>" . $row['name'] . "</td><td>" . $row['crypt'] . "</td></tr>";
    }
    
    echo "</table>";
    echo "</body></html>";
    
    //close the db
    mysql_close();
    
    ?>
    
    It can be evoked on browser. The output is a table but w/o table frame.

    Is there any way adding a table frame? Thanks

    B.R.
    satimis
     
  4. falko

    falko Super Moderator ISPConfig Developer

    Try
    Code:
    <table border="1">
     
  5. satimis

    satimis New Member

    Hi falko,

    It works but;
    Code:
    "1"
    
    should be as;
    Code:
    <table border=1>
    
    with quote " "

    Edit: (correction)
    Should be;
    Code:
    without quote " "
    
    satimis
     
    Last edited: Jun 1, 2009
  6. falko

    falko Super Moderator ISPConfig Developer

    Try
    Code:
    echo "<table border=\"1\"><tr><td>id</td><td>name</td><td>crypt</td>
    </tr>";
    or
    Code:
    echo '<table border="1"><tr><td>id</td><td>name</td><td>crypt</td></tr>';
     

Share This Page