listing results?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

listing results?

Post by me! »

I know this is a stupid question but how do I make this work like I want it to. I know I have done it before but for some reason I cant see it tonight. :crazy:

What I want is to get the event_id and event name from the events table and list them as ID event. (only two things in the table)
This is what i have but is does not exactly work.

Code: Select all

//get events from db
		$results = mysql_query("SELECT * FROM events");
			while ($mysql = mysql_fetch_array($results)) 
						{
						foreach( $mysql as $id => $event){
								echo "ID: $id, Event: $event <br />";
							}

						}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: listing results?

Post by social_experiment »

Try :

Code: Select all

<?php $results = mysql_query("SELECT * FROM events");
 while ($mysql = mysql_fetch_array($results)) {
  echo 'ID : '.$mysql['id'];
  echo 'Event : '.$mysql['event'];
  echo '<br />';
 }
 ?>
You should also check if there are actually data inside the table and depending on that result either echo the data or a message indicating 0 rows in the table.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: listing results?

Post by me! »

Yep it's amazing how sleep can help with things, just look at this again and came up with:

Code: Select all

//get events from db
		$results = mysql_query("SELECT * FROM events");
			while ($mysql = mysql_fetch_array($results)) 
						{
						echo "ID: ".$mysql["id"]." and the event is ".$mysql["event"]." <br />";
						}
I kept looking at this last night and it was not coming to me, but I knew I had done it hundreds of times before! :wink:

Thanks for the reply social_experiment.
Post Reply