Mysidia Adoptables Support Forum  

Home Community Mys-Script Creative Off-Topic
Go Back   Mysidia Adoptables Support Forum > Mysidia Adoptables > Tutorials and Tips

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 01-27-2011, 04:30 PM
HIddenPanda's Avatar
HIddenPanda HIddenPanda is offline
I Am... Da Sheep
 
Join Date: Jul 2010
Posts: 52
Gender: Male
Credits: 5,276
HIddenPanda
Post PHP/MySQL Tutorial

Im just making this as a resource for people who do not know how to/can't remember how to make a loop for mysql data in PHP

READ THIS FIRST: Kae's Mysql and PHP Basics

This thread will show you how to utilize the while function in php loops to help with outputting mysql data.

1. How to setup the while loop for data processing

PHP Code:
$i=0// This is what we will use to know where we are in our loop

$num mysql_num_rows($mysqlresults); // Can be named whatever you want and is compared to $i to know where we are 
The two variables above are very important...

To get mysql data into a variable, it's basically the same as putting a number in a variable

PHP Code:
$mysqlresults mysql_query($query); 
Now we must initiate the while loop :D

PHP Code:
while($i $num){
 
// Our processing will go here

Now if your wondering why we used a < symbol instead of a <= is because mysql row numbers start at 0 and increase by 1

Let's pretend we have 2 columns in our table
  • firstname
  • lastname
We currently have 5 rows inserted into our table also...

  • firstname lastname
  • Glenn Quagmire
  • Peter Griffin
  • Joe Swanson
  • Cleveland Brown
  • Robo Chicken
Were are going to make our page output for each row: Hello My Name is (firstname) of the (lastname) family.

A nifty little function we will be using is called mysql_result()

Heres basically how it works

PHP Code:
$variable mysql_result($mysql_data,$rownumber,$columnname); 
In this case we will have to use it twice, and your $rownumber will be $i

Also, before i forget If you don't already know, there is a nice shortcut for upping your $vars by 1, its $var++;

Now let's start the actual coding

First Let's get our data from our table and initiate the while loop

PHP Code:
$names mysql_query("SELECT * FROM ".$prefix."tablename"); // As you should know this will get ALL rows from table '$prefix'tablename

$num mysql_num_rows($names); // Get how many rows (5)

$i=0// I don't put in spaces because... its a habit... and it technically takes more time to type

$allnames ""// This will hold all of the names when we are done

while($i $num){


OK, Now that that is ready, lets get the data from the row's (specified by $i) data in firstname

PHP Code:

while($i $num){

$firstname mysql_result($names,$i,"firstname");


This stores the rows firstname into a var to make it easier to manipulate

Now we get the second row:

PHP Code:

while($i $num){
 
$firstname mysql_result($names,$i,"firstname");

$lastname mysql_result($names,$i,"lastname");

 } 
There, we are getting both columns of each row

Now we use a simple method to echo the results onto the page

PHP Code:

while($i $num){
  
 
$firstname mysql_result($names,$i,"firstname");
 
$lastname mysql_result($names,$i,"lastname");

echo 
"Hello, my name is ".$firsname." of the ".$lastname." family.<br>";

  } 
The output of this would be

Code:
Hello, my name is Glenn of the Quagmire family.
Hello, my name is Peter of the Griffin family.
Hello, my name is Joe of the Swanson family.
Hello, my name is Cleveland of the Brown family.
Hello, my name is Robo of the Chicken family.
And there ya go, that's how you use while loops to output mysql data :D

I hope this helped
__________________
dasheep.tk
Reply With Quote
Reply

Thread Tools
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Gradient Map Tutorial Whimsy Art Gallery 0 10-20-2014 03:55 PM
OOP Tutorial request Ruinily Suggestions and Feature Requests 4 02-09-2013 12:42 PM
Basic PHP & MySQL Tutorial Kaeliah Tutorials and Tips 8 03-23-2011 06:50 PM
Intermediate PHP & MySQL Tutorial I Kaeliah Tutorials and Tips 4 01-31-2011 03:20 PM
Quick php tutorial Ajof Tutorials and Tips 18 11-08-2010 11:20 AM


All times are GMT -5. The time now is 01:30 AM.

Currently Active Users: 711 (0 members and 711 guests)
Threads: 4,080, Posts: 32,024, Members: 2,016
Welcome to our newest members, jolob.
BETA





What's New?

What's Hot?

What's Popular?


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
vBCommerce I v2.0.0 Gold ©2010, PixelFX Studios
vBCredits I v2.0.0 Gold ©2010, PixelFX Studios
Emoticons by darkmoon3636