hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : n00b MySQL question
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

n00b MySQL question

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 11-16-2003, 02:14 AM
croakingtoad croakingtoad is offline
Aspiring Evangelist
 
Join Date: Jan 2003
Location: Roanoke, VA, US
Posts: 390

n00b MySQL question


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.

Reply With Quote


Sponsored Links
  #2  
Old 11-16-2003, 03:05 AM
digitok digitok is offline
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

Reply With Quote
  #3  
Old 11-16-2003, 03:05 AM
Burhan Burhan is offline
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

Reply With Quote
Sponsored Links
  #4  
Old 11-17-2003, 01:40 AM
croakingtoad croakingtoad is offline
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!

Reply With Quote
  #5  
Old 11-17-2003, 05:16 AM
Burhan Burhan is offline
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

Reply With Quote
  #6  
Old 11-22-2003, 06:40 PM
croakingtoad croakingtoad is offline
Aspiring Evangelist
 
Join Date: Jan 2003
Location: Roanoke, VA, US
Posts: 390
How do I keep it from echoing the field name?

http://www.lewisgaleclinic.com/skunk...pecialties.php

Reply With Quote
  #7  
Old 11-22-2003, 08:37 PM
digitok digitok is offline
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

Reply With Quote
  #8  
Old 11-22-2003, 09:28 PM
croakingtoad croakingtoad is offline
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 />";

Reply With Quote
  #9  
Old 11-23-2003, 02:01 AM
digitok digitok is offline
Web Hosting Master
 
Join Date: Jan 2003
Location: Perth, WA, Australia
Posts: 1,276
echo $val.'<br />';

__________________
nu-metal.org :: coming soon

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Rackspace Blog Looks at Easy Outsourcing for App Development Blog 2012-03-05 19:07:48
Web Host Rackspace Launches Private Beta for MySQL Cloud Database Web Hosting News 2011-12-01 21:09:51
Parallels Now Offering Licenses for CloudLinux OS Web Hosting News 2011-10-26 17:29:25
Web Hosting Software Firm CloudLinux Releases CloudLinux OS Version 6.1 Web Hosting News 2011-09-13 14:08:49
Web Host FireHost Partners with Cloud Security Firm Gazzang for Data Encryption Web Hosting News 2011-08-16 20:33:43


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?