从MySQL在PHP中创建数组 [英] Create array in PHP from mysql

查看:263
本文介绍了从MySQL在PHP中创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我可能已经尝试了一些睡眠不足的尝试,但是在从mysql查询创建数组时遇到了问题.我有这样的查询:

I think that I may be over trying things with little sleep but I am having problems creating arrays from mysql queries. I have a query like so:

$result = mysqli_query($con,"SELECT * FROM vehicles ORDER BY id");
while($row = mysqli_fetch_array($result)) {
    $description = $row['description'];
    $description = strtoupper($description);
    $id = $row['id'];
}

我希望它像这样创建一个数组:

I want it to create an array like so:

$v[1] = "Myarray1";

我已经尝试过了,但是没有用:

I have tried this but it does not work:

$v[ $row['id']] = $row;

我必须这样查询吗?

while( $row = mysql_fetch_assoc( $result)){}

如果可以的话,如何根据需要创建数组?

and if I do, how I do create the array as I need it?

非常感谢

推荐答案

您根本不会在此处创建数组.在您的循环中,您只是在修改一些您无法发布的上下文中的变量.

At no point are you creating an array here. In your loop you are simply modifying some variables that in the context you posted I cannot reason on.

如果需要的话,将不会进行任何数据过滤:

If all you need, no data filtering whatsoever this will do:

$list = Array();
while( $row = mysqli_fetch_array($result) ) {
   $list[] = $row;
}

[]基本上在PHP中附加",这是一个丑陋的机制,但有效.

[] basically 'appends' in PHP, it is an ugly mechanism but it works.

如果要访问每个主键的行(我认为是id),则只需将[]替换为[$row["id"]]

If you want to access rows per primary key ( id in your case I think ) then simply replace [] with [$row["id"]]

这篇关于从MySQL在PHP中创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆