PDA

View Full Version : Question on print data on MySQL table


satimis
29th May 2009, 11:15
Hi folks,

I have a php script;

# cat /home/satimis/sqlusers.php
<?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 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

falko
30th May 2009, 13:18
I tried putting the script on /var/www/ and renamed it as "index.html"

Rename it to index.php instead and try again.

satimis
30th May 2009, 13:40
Rename it to index.php instead and try again.
Hi falko,

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

# cat /var/www/sqlusers.php
<?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

falko
31st May 2009, 15:16
Try <table border="1">

satimis
1st June 2009, 04:16
Try <table border="1">Hi falko,

It works but;
"1"


should be as;
<table border=1>


with quote " "

Edit: (correction)
Should be;
without quote " "


satimis

falko
1st June 2009, 14:36
Try
echo "<table border=\"1\"><tr><td>id</td><td>name</td><td>crypt</td></tr>";
or
echo '<table border="1"><tr><td>id</td><td>name</td><td>crypt</td></tr>';