duanao4729 2013-04-11 01:51
浏览 38
已采纳

将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.

  • 写回答

1条回答 默认 最新

  • dongtigai3875 2013-04-11 01:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计