469,917 Members | 1,489 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

Displaying data in html table.

Hello,

I wondered if anyone could offer some guidance, I trying to write a
php script to connect to a database, and display the records in a
table.

I found the code here in a php4 text, and when I run this directly
through the php intrupeter, the line Successfully connected is display
and 4 as the number of rows.

The database table we connect to, has three field username, firstname,
surname.

When I tried a while loop, to display the data in a table. No values
were displayed.

Could anyone possibly demonstrate how to display the username,
firstname, surname values in a basic table?

Thank you
<?php
function db_connect(){
global $MYSQL_ERRNO, $MYSQL_ERROR;

$link_id = mysql_connect("localhost","user04", "password04");

if(!$link_id){
$MYSQL_ERROR = "Connection failed";
return 0;
}
else if(empty($dbname) && !mysql_select_db("database04")){
$MYSQL_ERROR = mysql_error();
return 0;
}
else return $link_id;
}

!$link_id = db_connect();
if(!$link_id) die(error_message('Error connecting'));

$result = mysql_query("SELECT * FROM users");
if(!$result) error_message('Error in selection');
else echo "Successfully connected. ";

$query_data = mysql_fetch_row($result);
$total_records = $query_data[0];

if(!$total_records) error_message('No records');
else echo "$total_records.";
?>
Jul 17 '05 #1
2 2074
Antoni wrote:
I wondered if anyone could offer some guidance, I trying to write a
php script to connect to a database, and display the records in a
table.


I suggest you have a lookie at the PEAR repository, and specifically
the DB and HTML_Table packages. They will make this a lot easier. Just
follow the examples in the docs. You are trying to do something funny
in the code you provided.

/M
Jul 17 '05 #2
On 15 May 2004 07:27:07 -0700, Antoni wrote:
Hello,

I wondered if anyone could offer some guidance, I trying to write a
php script to connect to a database, and display the records in a
table.

I found the code here in a php4 text, and when I run this directly
through the php intrupeter, the line Successfully connected is display
and 4 as the number of rows.

The database table we connect to, has three field username, firstname,
surname.

When I tried a while loop, to display the data in a table. No values
were displayed.

Could anyone possibly demonstrate how to display the username,
firstname, surname values in a basic table?

Thank you
<?php
function db_connect(){
global $MYSQL_ERRNO, $MYSQL_ERROR;

$link_id = mysql_connect("localhost","user04", "password04");

if(!$link_id){
$MYSQL_ERROR = "Connection failed";
return 0;
}
else if(empty($dbname) && !mysql_select_db("database04")){
$MYSQL_ERROR = mysql_error();
return 0;
}
else return $link_id;
}

!$link_id = db_connect();
if(!$link_id) die(error_message('Error connecting'));

$result = mysql_query("SELECT * FROM users");
if(!$result) error_message('Error in selection');
else echo "Successfully connected. ";

instead of this : $query_data = mysql_fetch_row($result);
$total_records = $query_data[0]; use this:
$total_records = mysql_num_rows($result);

if(!$total_records) error_message('No records');
else echo "$total_records.";
and here add:

for($i=0;$i<$total_records;$i++)
{
$query_data = mysql_fetch_row($result);
echo $query_data[0],$query_data[1],$query_data[2];
// or whatever you want to do with data

}
?>


and that's it.
array $query_data has your data as stated in table definition.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Gregory | last post: by
2 posts views Thread by Steven | last post: by
12 posts views Thread by Charles Astwood | last post: by
3 posts views Thread by Al Wilkerson | last post: by
2 posts views Thread by Fedor G via DotNetMonster.com | last post: by
3 posts views Thread by CLEAR-RCIC | last post: by
1 post views Thread by Christopher Brandsdal | last post: by
11 posts views Thread by dba | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.