Page 1 of 1
Values from an SQL database into a php array.
Posted: Wed Apr 28, 2010 3:45 pm
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.
Re: Values from an SQL database into a php array.
Posted: Wed Apr 28, 2010 5:18 pm
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.
Re: Values from an SQL database into a php array.
Posted: Wed Apr 28, 2010 6:11 pm
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
i am assuming this is what you are looking for....
Re: Values from an SQL database into a php array.
Posted: Wed Apr 28, 2010 6:58 pm
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

Re: Values from an SQL database into a php array.
Posted: Wed Apr 28, 2010 8:29 pm
by hallsofvallhalla
I am not sure he will have to clarify, guess I am confused

Re: Values from an SQL database into a php array.
Posted: Thu Apr 29, 2010 3:45 am
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.
Re: Values from an SQL database into a php array.
Posted: Thu Apr 29, 2010 10:18 am
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'.
Re: Values from an SQL database into a php array.
Posted: Thu Apr 29, 2010 10:27 am
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);
....}
}
Re: Values from an SQL database into a php array.
Posted: Thu Apr 29, 2010 4:21 pm
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.
Re: Values from an SQL database into a php array.
Posted: Fri Apr 30, 2010 4:21 am
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
