doumie6223 2013-02-14 11:10
浏览 39
已采纳

从数据库中制作一个舒适的php多维数组

i got a code like this:

include 'dbconnect.php';
mysql_query("SET NAMES utf8;"); 

$sql = mysql_query("SELECT * From courseEnglish");
$courseInfo = array();


while ($row_course = mysql_fetch_assoc($sql))
   $courseInfo[] = $row_course;

it gives me a php array from the db, but in order to present the data i need to use:

echo $courseInfo[2]['Course'];

i would like for the array to be so i can just type somthing like:

echo $courseInfo[2][2];

is there an option for changing the code so ill have an array i can loop on with just indexes?

  • 写回答

3条回答 默认 最新

  • doudong3570 2013-02-14 11:13
    关注

    I think that you're looking for mysql_fetch_row, which does the same as mysql_fetch_assoc, except for the fact that it returns a numerically indexed array. here's the link to the docs.

    Having said that, you might want to consider ditching the deprecated mysql_* extension, in favour of either mysqli_* (the i stands for improved), or the fully OO-style PDO extension. Check the red box at the top of any mysql_* function's manual page, it contains links to the preferred counterpart-function...

    But all things considered, looking at your query (SELECT * FROM ...), I'd not use a numerically indexed array at all to get the results. If your table contains more than 1 field, you'll hit trouble sooner or later, when trying to work out what piece of data is coming from what field. Seriously... just use an assoc array, or even better: use objects.
    Suppose you change the table layout (add/remove certain fields?), it'll be hell to maintain your code in that case. Still, it's up to you to decide, but I thought I'd might mention this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程