Page 1 of 1

PHP and Mysql Handle Multiple Results

Posted: Sat Jul 07, 2012 8:54 pm
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

Re: PHP and Mysql Handle Multiple Results

Posted: Sun Jul 08, 2012 1:14 am
by Xaleph
You mean mysl_fetch_assoc($result)?

while($row = mysql_fetch_assoc($result)){
// update all the villages
}

Re: PHP and Mysql Handle Multiple Results

Posted: Sun Jul 08, 2012 10:22 am
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 ?

Re: PHP and Mysql Handle Multiple Results

Posted: Sun Jul 08, 2012 1:49 pm
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?

Re: PHP and Mysql Handle Multiple Results

Posted: Sun Jul 08, 2012 1:50 pm
by xcalpro
This site might have what you're looking for http://www.w3schools.com/php/php_ref_mysql.asp

Re: PHP and Mysql Handle Multiple Results

Posted: Sun Jul 08, 2012 2:21 pm
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)

Re: PHP and Mysql Handle Multiple Results

Posted: Sun Jul 08, 2012 2:56 pm
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

Re: PHP and Mysql Handle Multiple Results

Posted: Sun Jul 08, 2012 3:13 pm
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?

Re: PHP and Mysql Handle Multiple Results

Posted: Sun Jul 08, 2012 4:46 pm
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.

Re: PHP and Mysql Handle Multiple Results

Posted: Mon Jul 09, 2012 3:16 pm
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"..