Jump to content

How to Loop PHP/MYSQL


lopes_andre

Recommended Posts

TABLE_NAMES

id | Name

----------

1  | Peter

2  | John

3  | Maria

 

TABLE_COUNTER

counter

1

 

I'am trying to make a loop for each time I run the "show_me_the_name.php" the script shows me a name, using two tables "TABLE_NAMES" and "TABLE_COUNTER"

 

For example:

 

On the first time I run the php script shows "Peter";

Second time, "John";

3º time, "Maria";

4º time, "Peter";

5º time, "John";

 

How can I start for get this done?

Link to comment
https://forums.phpfreaks.com/topic/129230-how-to-loop-phpmysql/
Share on other sites

Hi,

 

The script must be global. If I run the script I get the name of id1, if another person run the script, that person will get the id2 name. The counter will do this counting "OLD.id + 1" each time the php script runs.

 

How can I start? There are some examples on how to do this loops using php and mysql?

Link to comment
https://forums.phpfreaks.com/topic/129230-how-to-loop-phpmysql/#findComment-669990
Share on other sites

There won't be any loops....each time the page loads, i would run something like the following:

 

<?php
// Do db connection stuff here

//Get next record
$result = mysql_query("SELECT * FROM `TABLE_NAMES` WHERE `id` > (SELECT MAX(counter) FROM `TABLE_COUNTER`) LIMIT 1");
if(!$result){
  //handle for when it's maxed out
}
//Get the row (there will only be one)
$data = mysql_fetch_assoc($result);
//Print out the data for debugging
print_r($data);
//Update counter
mysql_query(sprintf("UPDATE `TABLE_COUNTER` SET `counter` = '%s'",$data['id']));

?>

Link to comment
https://forums.phpfreaks.com/topic/129230-how-to-loop-phpmysql/#findComment-669995
Share on other sites

Hi rhodesa,

 

I have not yet tested the code. But thanks a lot!

 

My english is bad, but I was try to apply the word "loop" to this:

 

1º time, show id=1

2º time, show id=2

3º time, show id=3

4º time, show id=1

5º time, show id=2

 

I have only 3 names in the database and i want to loop forever.

 

Best regards,

André.

Link to comment
https://forums.phpfreaks.com/topic/129230-how-to-loop-phpmysql/#findComment-670013
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.