0

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.

Brandon
  • 11
  • 2
  • you need to learn how to use arrays in php. – didierc Apr 11 '13 at 01:54
  • Why do you need separate variables for that? Keep data that belongs together in an array, it's much simpler. Try `mysql_fetch_assoc` instead of `_row` to get usable array indices. Also, the mysql extension is deprecated, stop using it now and learn mysqli or PDO. – deceze Apr 11 '13 at 01:54
  • http://php.net/manual/en/language.types.array.php – didierc Apr 11 '13 at 01:57
  • Thank you both for the words of wisdom. I still have a lot to learn with PHP and this project I'm working on is my way of forcing myself to learn through doing. I'm not good at reading tutorials or online learning guides, I'd much rather learn as I go. – Brandon Apr 11 '13 at 02:05
  • @Brandon You *need* to learn to read the manual though. Tutorials are only the first stepping stone to get into a topic at all. Pretty quickly the manual needs to take over as the main source of information. – deceze Apr 11 '13 at 02:22

1 Answers1

1

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Wow! That was a lot easier that I expected. Thanks for a very direct answer, and the information about deprecated mysql_* functions. I'll check into the two new functions now before I get too much further into the process and have to rewrite all of my SQL coding. – Brandon Apr 11 '13 at 02:03