
11-16-2003, 02:14 AM
|
|
Aspiring Evangelist
|
|
Join Date: Jan 2003
Location: Roanoke, VA, US
Posts: 390
|
|
I'm just beginning to really learn MySQL and PHP and have a question. First, what's a good online resource for MySQL reference and/or tutorials?
Okay, say I have a database called 'hospital'
Now, let's say I have 10 fields containing information.
How can I display this information on a webpage using PHP and specifying a certain order?
Say I want field 1 first, then field 2 then field 3, etc., all separated by <br>...
Thanks for any and all help.
|

11-16-2003, 03:05 AM
|
|
Web Hosting Master
|
|
Join Date: Jan 2003
Location: Perth, WA, Australia
Posts: 1,276
|
|
Did you mean you have a table called hospital?
And I assume you wish to grab all rows in the table, and display them...
So something like this:
PHP Code:
$q = mysql_query("SELECT f1,f2,f3 FROM hospital ORDER BY id ASC");
$r = mysql_num_rows($q);
if (!$r)
echo 'Table contains no rows.';
else {
while (list($f1,$f2,$f3) = mysql_fetch_array($q)))
echo $f1.'<br />'.$f2.'<br />'.$f3.'<br /><br />';
echo 'Total rows in table: '.$r;
}
Hope this helps.
__________________
nu-metal.org :: coming soon
|

11-16-2003, 03:05 AM
|
|
Community Guide
|
|
Join Date: Jul 2003
Location: Kuwait
Posts: 5,100
|
|
Best way to learn mysql is to read the manual (check my signature for the link). I'm sure there are a few tutorials on mysql that you can get from google and sites like phpfreaks. I also have a few tutorials on my website. You might also want to look at the section of the php manual that deals with mysql.
As to you other question :
A database contains tables, and tables contain fields. Databases don't contain fields. So assuming you have a database hospital with a table info that you want to display ... this will work.
PHP Code:
if (! ($status = mysql_connect("localhost", "user", "pass"))) { die(mysql_errno()." : ".mysql_error()); }
if (! ($status = mysql_select_db("hospital"))) { die(mysql_errno()." : ".mysql_error()); }
$query = "SELECT * FROM info";
if (! ($status = mysql_query($query))) {
die(mysql_errno()." : ".mysql_error()); }
while ($row = mysql_fetch_assoc($status))
{
while(list($key,$val) = each($row))
{
echo $key." ".$val."<br />";
}
}
mysql_close();
If you want to change the order in which the fields are displayed -- then you will have to read the mysql manual.
You can look up the manual references to each of the php functions and find out what's going on.
__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise
|

11-17-2003, 01:40 AM
|
|
Aspiring Evangelist
|
|
Join Date: Jan 2003
Location: Roanoke, VA, US
Posts: 390
|
|
Okay, I've implemented the second example from fyrestrtr (digitok: I tried yours, couldn't get it to work) and it seems to work, but it's listing everything in the table, which I don't want, but I think I asked for that so my bad.
Basically, I've got a database called 'hospital'. In that database, there is a table called 'specialties'. In that table are several fields, field1, field2, field3, etc.
How can I select only one or two of those fields for display and not all? Also, how to I apply a style to the output?
Thanks!
|

11-17-2003, 05:16 AM
|
|
Community Guide
|
|
Join Date: Jul 2003
Location: Kuwait
Posts: 5,100
|
|
SELECT field1, field2, field3 FROM specialities;
You can apply style just like you would to any other echo statement.
PHP Code:
echo "<b>".$key."</b><i>".$val."</i><br />";
etc.
__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise
|

11-22-2003, 06:40 PM
|
|
Aspiring Evangelist
|
|
Join Date: Jan 2003
Location: Roanoke, VA, US
Posts: 390
|
|
|

11-22-2003, 08:37 PM
|
|
Web Hosting Master
|
|
Join Date: Jan 2003
Location: Perth, WA, Australia
Posts: 1,276
|
|
Umm... Remove the $key from the echo?
__________________
nu-metal.org :: coming soon
|

11-22-2003, 09:28 PM
|
|
Aspiring Evangelist
|
|
Join Date: Jan 2003
Location: Roanoke, VA, US
Posts: 390
|
|
Okay, I tried the below but it didn't work...I'm not good at PHP, can you help me with the syntax?
echo ".$val."<br />";
|

11-23-2003, 02:01 AM
|
|
Web Hosting Master
|
|
Join Date: Jan 2003
Location: Perth, WA, Australia
Posts: 1,276
|
|
__________________
nu-metal.org :: coming soon
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| Postbit Selector |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|