croakingtoad
11-16-2003, 02:14 AM
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.
digitok
11-16-2003, 03:05 AM
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:
$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.
Burhan
11-16-2003, 03:05 AM
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 (http://www.google.com/search?q=mysql+php+tutorial) and sites like phpfreaks (http://www.phpfreaks.com). I also have a few tutorials on my website. You might also want to look at the section of the php manual (http://www.php.net/mysql) 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.
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 (http://www.mysql.com/doc/en/SELECT.html).
You can look up the manual references to each of the php functions and find out what's going on.
croakingtoad
11-17-2003, 01:40 AM
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!
Burhan
11-17-2003, 05:16 AM
SELECT field1, field2, field3 FROM specialities;
You can apply style just like you would to any other echo statement.
echo "<b>".$key."</b><i>".$val."</i><br />";
etc.
croakingtoad
11-22-2003, 06:40 PM
How do I keep it from echoing the field name?
http://www.lewisgaleclinic.com/skunkworks/specialties.php
digitok
11-22-2003, 08:37 PM
Umm... Remove the $key from the echo?
croakingtoad
11-22-2003, 09:28 PM
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 />";