Log in

View Full Version : PHP/MySQL Tutorial


HIddenPanda
01-27-2011, 04:30 PM
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 (http://www.mysidiaadoptables.com/forum/showthread.php?t=1700)

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


$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

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


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


$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


$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



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:



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



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


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