Database Forum
Web Database Register Members List Calendar FAQ

 
Go Back   Database Forum > Database Management > PHP & MySQL
User Name
Password

Reply
 
Thread Tools Search this Thread Display Modes
Default Display a mysql table using php
03-19-2005, 11:17 AM #1  

clifford
Senior Member

 
Status: Offline
Posts: 266
Join Date: Mar 2005
Location: San Fran, CA

Hello All,

I need to create a php script to display my mysql table and certain fields. Any assistance is much appreciated.

thanks
Reply With Quote

Default
03-19-2005, 01:36 PM #2  

cu-it-up
Member

 
Status: Offline
Posts: 33
Join Date: Mar 2005

Here is an example. Replace the connection and table variables with your own.

viewall.php
PHP Code:
<html>
<head><title>Your Page Title</title></head>
<body>
<?php
$database
="yourdbname";
mysql_connect ("localhost", "yourusername", "yourpassword");
@
mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query( "SELECT * FROM yourtable" )
or die(
"SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
print
"There are $num_rows records.<P>";
print
"<table width=400 border=1>\n";
while (
$get_info = mysql_fetch_row($result)){
print
"<tr>\n";
foreach (
$get_info as $field)
print
"\t<td><font face=arial size=1/>$field</font></td>\n";
print
"</tr>\n";
}
print
"</table>\n";
?>
</body>
</html>
viewcertainfields.php
PHP Code:
<html>
<head><title>Your Page Title</title></head>
<body>
<?php
$database
="yourdbname";
mysql_connect ("localhost", "yourusername", "yourpassword");
@
mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query( "SELECT field1, field2, field3 FROM yourtable" )
or die(
"SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
print
"There are $num_rows records.<P>";
print
"<table width=200 border=1>\n";
while (
$get_info = mysql_fetch_row($result)){
print
"<tr>\n";
foreach (
$get_info as $field)
print
"\t<td><font face=arial size=1/>$field</font></td>\n";
print
"</tr>\n";
}
print
"</table>\n";
?>
</body>
</html>
You can also add limit and order to the query...
Reply With Quote

Unhappy I get an error message
05-17-2005, 04:17 PM #3  

andreabenn
Junior Member

 
Status: Offline
Posts: 1
Join Date: May 2005

Hello,

I am VERY new to PHP. I tried the script you suggested to display all the tables in the database and it reported an error like this:

SELECT Error: Table 'shock05_vpoints.VIP' doesn't exist.

I successfully changed the information for the database name, user and password, but I have a problem with the following line...

$result = mysql_query( "SELECT * FROM yourtable" )

Is there something I should change in this line? I changed "yourtable" in that line to the name of the only table in my database and that is where I get the error.

Also, is there anything else I should change in the script. Walk me through this please, I am new to this.
Reply With Quote

Question
11-04-2005, 12:14 PM #4  

Tom
Junior Member

 
Status: Offline
Posts: 2
Join Date: Sep 2005
Location: Port Saint Lucie, Florida

Hey cu-it-up

I tried the first sample you provided earlier in this thread.
I named the file mytest.html and accessed it through my web server.
As provided, with my own info of course, the output is:

"; print "\n"; while ($get_info = mysql_fetch_row($result)){ print " \n"; foreach ($get_info as $field) print "\t $field \n"; print "
\n"; } print " \n"; ?>

If I remove the HTML wraper around the PHP code and rename the file to mytest.php, the output is exactly as you said it should be.

Why does the <html><body> ...... </body></html> cause problems?

Just trying to learn something. Thanks, Tom
Reply With Quote

Default
11-08-2005, 07:36 PM #5  

cu-it-up
Member

 
Status: Offline
Posts: 33
Join Date: Mar 2005

Either way you need to save the file as a .php file and not a .html file...
Reply With Quote

Default
04-26-2006, 04:28 PM #6  

Raeana
Junior Member

 
Status: Offline
Posts: 4
Join Date: Apr 2006

Quote:
Originally Posted by Ilya
The most simple way is download PHP generator (ex. from my signature - Mysql Data Wizard), then to correct php script if need... I.e. if there is no necessity for the forms for editing the data - simply remove or remarks this. The generator allows not simply show content of one table. It is capable by easy mouse click, join some tables, make aliases for field names etc.
OK Question.

I have 2 tables. Customers & Keys

I need to be able to click a particular customer & have all of the keys they have come up.

How do I do this?
Reply With Quote

Default
05-09-2006, 01:00 PM #7  

Raeana
Junior Member

 
Status: Offline
Posts: 4
Join Date: Apr 2006

Not possible?
Reply With Quote

Default
05-09-2006, 07:23 PM #8  

popye
Member

 
Status: Offline
Posts: 80
Join Date: Mar 2005

Have you created a foreign key in your Customers table in a field that points to a field in the keys table.
Reply With Quote

Default
08-08-2006, 12:47 PM #9  

Raeana
Junior Member

 
Status: Offline
Posts: 4
Join Date: Apr 2006

Quote:
Originally Posted by popye
Have you created a foreign key in your Customers table in a field that points to a field in the keys table.
Yes I have.
Reply With Quote

Default
08-08-2006, 01:16 PM #10  

Raeana
Junior Member

 
Status: Offline
Posts: 4
Join Date: Apr 2006

And Actually, I believe I have done that now. The whole database is crude, as I am just testing...but take a look at what I mean here...

I guess it is the search I have a problem with. I need to bring both the Customer & the Keys that are associated with that customer up on the same search, but I am not sure how to do that:

http://www.aallsafe.com/newmed/
Reply With Quote

Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Forum Jump



All times are GMT -5. The time now is 06:58 AM.

Powered by: vBulletin Version 3.7.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Database Forums

Help support our forum assistants and product development ->

go to top go to top