Values from an SQL database into a php array.

C++, C#, Java, PHP, ect...
Post Reply
Age of Fable
Posts: 54
Joined: Fri Feb 19, 2010 6:37 am

Values from an SQL database into a php array.

Post by Age of Fable »

I have an SQL database which has the 'character sheet' for my game.

I get the values from the database:

$result=mysql_query("SELECT * FROM current ORDER BY currentSL DESC");

[SL is Social Level, which is the main thing players are trying to increase]

I need to get these values into an array, so I can manipulate the values in php.

Can anyone tell me how to do this? I've looked at php.net, but I couldn't work it out.

I'm not able to download the tutorial videos.
Last edited by Age of Fable on Wed Apr 28, 2010 5:18 pm, edited 1 time in total.
Easy to play - hard to survive. http://www.ageoffable.net
Age of Fable
Posts: 54
Joined: Fri Feb 19, 2010 6:37 am

Re: Values from an SQL database into a php array.

Post by Age of Fable »

A clarification:

Someone's already told me how to get a single variable from a mysql database, if I have (eg) a particular username.

I need to be able to get the stats for each character, rather than looking for a specific one.
Easy to play - hard to survive. http://www.ageoffable.net
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Values from an SQL database into a php array.

Post by hallsofvallhalla »

Code: Select all

$playerinfo=""SELECT * FROM current ORDER BY currentSL DESC'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
then just call them by $playerinfo3['field']

field would be the field name in the table so if i had a field called strength

Code: Select all

$playerinfo3['strength']
i am assuming this is what you are looking for....
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Values from an SQL database into a php array.

Post by Jackolantern »

Isn't he pulling all of his characters into an array, though? Wouldn't he have to use a 2-dimensional array to first choose the player, and then which stat to look up? Or would it have to be found through a foreach statement? It has been a while since I have used PHP, so I can't remember :P
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Values from an SQL database into a php array.

Post by hallsofvallhalla »

I am not sure he will have to clarify, guess I am confused :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Values from an SQL database into a php array.

Post by Jackolantern »

I am just looking at his SQL query. Unless I am missing something, he isn't using any WHERE clause, so won't it pick up everything in the table, in descending order based on the "currentSL" field?

Anyway, even if that isn't what he is really wanting to do, how do you handle that in PHP again? My game never pulled multiple rows at one time, since I was always using a WHERE clause or LIMITing the results. I read in a PHP book how you could hold more than one row at a time in an array, but I don't remember how you accessed the data.
The indelible lord of tl;dr
Age of Fable
Posts: 54
Joined: Fri Feb 19, 2010 6:37 am

Re: Values from an SQL database into a php array.

Post by Age of Fable »

Yes, this:

$result=mysql_query("SELECT * FROM current ORDER BY currentSL DESC");

gets the entirety of a two-dimensional database: multiple attributes for multiple characters.

I need to be able to display information for every character, because this is a 'leader-board'.
Easy to play - hard to survive. http://www.ageoffable.net
Age of Fable
Posts: 54
Joined: Fri Feb 19, 2010 6:37 am

Re: Values from an SQL database into a php array.

Post by Age of Fable »

I ended up doing this:

$result=mysql_query("SELECT * FROM current");
$cnum=mysql_num_rows($result);

$attributes=array("null","name","estate","parentslive","eldest","funds","allowance","temperament","currentSL","startSL","bargaining","presence",
"appearance","duelling","paramour","luck");

for ($z=1;$z<16;$z++) {
....$y=mysql_query("SELECT ".$attributes[$z]." FROM current",$link);
....for ($x=0;$x<$cnum;$x++) {
........$char[$x+1][$z]=mysql_result($y,$x);
....}
}
Easy to play - hard to survive. http://www.ageoffable.net
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Values from an SQL database into a php array.

Post by hallsofvallhalla »

ahhh i see, you can actually use a while statement to make less calls on the database.

http://www.php-mysql-tutorial.com/wikis ... abase.aspx

i actually have a much simpler way in my video tutorials but I am not in a place I can grab it and post it. Will later.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Values from an SQL database into a php array.

Post by Jackolantern »

Also, Age, you can keep your code in its proper formatting without having to use the dots if you wrap it in the "code" tags :)
The indelible lord of tl;dr
Post Reply

Return to “Coding”