将MySQL数组拆分为单独的php变量[关闭]
I'm fairly new to PHP, so please excuse me if I'm asking the wrong question or asking something terribly simple.
I have a MySQL table that holds static data about base stats for players. I need to display these stats individually in the player's overview screen, but I'm having trouble separating the data after the query returns the results.
Here is my current code:
$basestatsquery = mysql_query("
SELECT Army, Defense, Resource, Food, Trade_Goods, Technology
FROM Player_Stats_Base
") or die(mysql_error());
$basestats = mysql_fetch_row($basestatsquery);
And I've been able to verify that it's pulling data properly by using the following in my page:
<p><?php print_r($basestats) ?></p>
Which outputs the results as an array and is displayed as such:
Array ( [0] => 5 [1] => 5 [2] => 250 [3] => 60 [4] => 0 [5] => 0 )
But from this point I can't figure out where to go. I need each of these 6 values to be separated into their own variable so that I can display them in the proper location and eventually multiply them by modifiers that are meant to increase base stats.
Any help would be much appreciated.
Use mysql_fetch_assoc()
it's more intuitive *
$basestats = mysql_fetch_assoc($basestatsquery);
echo $basestats['Army'];
echo $basestats['Defense'];
echo $basestats['Resource'];
echo $basestats['Food'];
echo $basestats['Trade_Goods'];
echo $basestats['Technology'];
*Please, don't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.