PHP and Mysql Handle Multiple Results

C++, C#, Java, PHP, ect...
Post Reply
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

PHP and Mysql Handle Multiple Results

Post by vitinho444 »

Hello community :D

Ok, i used to use this "function" a lot but now i just forgot it...

It goes like this, im trying to use the runcron to update all the villages.

So i do this (diagram)

Query Players Villages -> Check Number of Results ->

Now i wanna know what function for mysql using a while loop or something i need to use, for update the first result (first village), then the second, then third and even if there are more..

I just forgot.. it was something like: mysql_something($i, "Table ROW", $query);

Anyone knows this?

Thanks
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: PHP and Mysql Handle Multiple Results

Post by Xaleph »

You mean mysl_fetch_assoc($result)?

while($row = mysql_fetch_assoc($result)){
// update all the villages
}
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: PHP and Mysql Handle Multiple Results

Post by vitinho444 »

Well i dont think so...

How can i use that one to update more than 1 village in a loop?

Also, is there any manual with all the mysql functions ?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Sharlenwar
Posts: 523
Joined: Mon May 28, 2012 7:14 pm

Re: PHP and Mysql Handle Multiple Results

Post by Sharlenwar »

I'm not too sure what sort of commands you would have in php, but if you have any sort of looping, then you would simply make a loop to iterate through all your villages to update them.

Or is it something else you are after?
Deep within the Void of Quasion, a creation.

**My Corner of the Web**
***NEW***GrindFest - My own PHP/MySQL game!
Sharlenwar's Adventures
Torn-City - Massively multiplayer online text based RPG
User avatar
xcalpro
Posts: 65
Joined: Sat May 19, 2012 10:25 pm

Re: PHP and Mysql Handle Multiple Results

Post by xcalpro »

This site might have what you're looking for http://www.w3schools.com/php/php_ref_mysql.asp
Skills: HTML5, JavaScript, PHP, SQL, Python, BASIC, HeroEngine(HSL), AGS(Lua), Unity, Photon, 3D Max, Mudbox, Photoshop, Poser, Flash
"Jack-of-all-Trades, Master of none"
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: PHP and Mysql Handle Multiple Results

Post by vitinho444 »

Well, i checked w3schools before post but couldnt find the function..
But hey, if you got any other ideas.

The goal is simple, i got 2 or more things to update.

Like : $query = "SELECT * from villages where owner= $playerid"

So if the player has 2 or more villages.. how can i update each one (getting each field for each village)
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: PHP and Mysql Handle Multiple Results

Post by Chris »

I know people could even do that using only SQL, but since I'm not much of an SQL guy I do it with PHP. Xaleph already posted what you need:

Code: Select all

$userVillagesQuery = mysql_query("SELECT * FROM `villages` WHERE `owner_id` = $playerid");

// loop through each village
while( $village = mysql_fetch_assoc($userVillagesQuery) )
{
    // do update
    mysql_query("UPDATE `villages` SET `iron` = '" . ($village['iron'] + 5000) . "' WHERE `id` = {$village['id']}");
} 
All the PHP documentation can be found at http://php.net/

Here's the mysql docu: http://php.net/mysql
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: PHP and Mysql Handle Multiple Results

Post by vitinho444 »

Ok sorry xaleph i didnt understood how to use the assoc function.

I used now, but got a problem.. i think the while loop is not being completed :S Because in the pages that i got include runcron the content of the page doesnt show...

do i need a $i ++; or smth?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: PHP and Mysql Handle Multiple Results

Post by Xaleph »

Nope, the while loop takes care of that for you, or actually, mysql_fetch_assoc() does, if nothing is found, false will return, so the while() breaks. If you don`t get any content, then either the query is wrong, or the included runcron() function.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: PHP and Mysql Handle Multiple Results

Post by vitinho444 »

well, i got this:

Code: Select all

$aldeias = mysql_query("SELECT * from villages where username= '$username'");
        $aldeias_result = mysql_fetch_array($villages);
        $aldeias_result_number = $mysql_num_rows($villages);
       
       
        while($village = mysql_fetch_assoc($aldeias))
        {
            echo "working";
        }
and still no thing is showed.. not even "working"..
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Post Reply

Return to “Coding”