Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Displaying a mysql table on a webpage
Old 12-09-2009, 10:31 PM Displaying a mysql table on a webpage
Junior Talker

Posts: 2
Name: Chris
Trades: 0
Hi,

Just wanted to know what exact scripting would allow me to display a table created in myPHPAdmin onto a webpage?
ceevee is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-10-2009, 03:32 AM Re: Displaying a mysql table on a webpage
Experienced Talker

Posts: 48
Name: Pieter
Location: Holland
Trades: 0
PHP Code:
<?
 
mysql_connect
(`.`, `.`,  `.`);
mysql_select_db(database);
 
//Select all items:
$query mysql_query("SELECT * FROM `users`");
 
//Get the amount of rows
$rows mysql_num_rows($query);
 
//open table
echo"<table>";
 
//make a for loop
for ($i 0$i <= ($rows -1); $i++)
{
$data mysql_fetch_object($query);
?>    
    <tr>
        <td>Name</td>
        <td><?php echo"$data->name"?></td>
    <tr>  
    <tr>
        <td>Lastname</td>
        <td><?php echo"$data->lastname"?></td>
    <tr>
<?
}
 
//close table
echo"</table>
Good luck
elcosmo is offline
Reply With Quote
View Public Profile
 
Old 12-10-2009, 07:19 AM Re: Displaying a mysql table on a webpage
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Elcosmo code is a good start, but you need to modify it for each query, to list the fields.

I did not have tested this, and written without PHP on hands, but it should adapt itself by fetching a list of the columns first, add a header to the table, and then output the rows:
PHP Code:
<?php
 
mysql_connect
(`.`, `.`,  `.`);
mysql_select_db(database);
 
//Select all items:
$query mysql_query("SELECT * FROM `users`");
$rows mysql_fetch_object($query);

//open table
echo"<table>";

//get the columns name
$aryCols=array();
foreach(
$rows as $key=>$val){
    
$aryCols[]=$key;
}

//print a header in your table
echo"<tr>";
foreach(
$aryCols as $name){
    echo 
html<<<
<
th>{$name}</th>
html;
echo 
"</tr>";

//parse each rows
while($obj=foreach($rows as $row)){
    echo
"<tr>";
    
    
//fetch the columm values
    
foreach($aryCols as $name){
        echo 
html<<<
        <
td>{$obj->$name}</td>
html;
    }
    
    echo
"</tr>";
}

//close table
echo"</table>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Displaying a mysql table on a webpage
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11490 seconds with 12 queries