Find the answer to your Linux question:
Results 1 to 7 of 7
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); ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,801

    How to display data on mysql_table in table form


    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:
    satimis@satimis.com<br>satimis<br>0e3QZ140ZrOOE<br>smsliu@satimis.com<br>smsliu<br>0eWBW8//TnfEg<br>albert@satimis.com<br>albert<br>XNP5IEvi8VZS6<br>patricia@satimis.com<br>patricia<br>0eg78AAkIGLWM<br>
    postmaster@satimis.com<br>postmaster<br>0e130m.vyL/aU<br>albertcheung@satimis.com<br>Albert Cheung<br>XXvTmrqgLrBGY<br>root@vz2:/#
    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. #2
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,561
    Do you mean

    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);
    echo '<table>;
    while ($row = mysql_fetch_assoc ($result) ) { // loop through the result set
        echo '<tr>';
        echo "<td>{$row["id"]}</td>";
        echo "<td>{$row["name"]}</td>";
        echo "<td>{$row["crypt"]}</td>";
        echo '</tr>';
    }
    echo '</table>';
    ?>
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    The Fifth Continent

  3. #3
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,801
    Quote Originally Posted by elija View Post
    Do you mean

    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);
    echo '<table>;
    while ($row = mysql_fetch_assoc ($result) ) { // loop through the result set
        echo '<tr>';
        echo "<td>{$row["id"]}</td>";
        echo "<td>{$row["name"]}</td>";
        echo "<td>{$row["crypt"]}</td>";
        echo '</tr>';
    }
    echo '</table>';
    ?>
    Hi elija,

    Thanks for your advice.

    Put your script file "sqlusers.php" on /var/www/sqlusers.php. Evoke it on browser. It prompts;
    Code:
    Parse error: syntax error, unexpected '>' in /var/www/sqlusers.php on line 14
    Line 14 is;
    Code:
        echo '<tr>';
    B.R.
    satimis

  4. #4
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,561
    (Blush)
    Code:
    echo '<table>;
    should be
    Code:
    echo '<table>';
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    The Fifth Continent

  5. #5
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,801
    Quote Originally Posted by elija View Post
    (Blush)
    Code:
    echo '<table>;
    should be
    Code:
    echo '<table>';
    Sorry, still fail.


    Code:
    Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in /var/www/sqlusers.php on line 8
    
    Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /var/www/sqlusers.php on line 9
    
    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/sqlusers.php on line 13

    line 8
    Code:
    $connect=mysql_connect($host,$user,$pass); //Connect to the database.
    line 9
    Code:
    mysql_select_db($dbase, $connect);
    line 13
    Code:
    while ($row = mysql_fetch_assoc ($result) ) { // loop through the result set

    # cat /var/www/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);
    echo '<table>';
    while ($row = mysql_fetch_assoc ($result) ) { // loop through the result set
        echo '<tr>';
        echo "<td>{$row["id"]}</td>";
        echo "<td>{$row["name"]}</td>";
        echo "<td>{$row["crypt"]}</td>";
        echo '</tr>';
    }
    echo '</table>';
    
    ?>
    B.R.
    satimis

  6. #6
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,561
    Access denied for user 'root'@'localhost'
    Can't help you with that I'm afraid unless you want to tell me all your passwords
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    The Fifth Continent

  7. #7
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,801
    Quote Originally Posted by elija View Post
    Can't help you with that I'm afraid unless you want to tell me all your passwords
    Hi elija,

    There is nothing to do with password.

    Following script works nicely displaying the output in table form but without table frame ;

    # 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();
    
    ?>
    I'm still looking around for solution adding frame to the table.


    B.R.
    satimis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •