从Mysql中获取数据并转换为PHP数组

编程入门 行业动态 更新时间:2024-10-22 13:35:45
本文介绍了从Mysql中获取数据并转换为PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望在一个我认为一定很简单的问题上得到一些帮助,但我已经被困了一段时间.

I hope to get some help on a problem that I suppose must be simple but I have been stuck for a while.

我有一个简单的 Mysql 表,其中包含 3 个字段(id、customer、sales).我只需要查询这个表并获得一个 php 数组.

I have a simple Mysql table with 3 fields ( id, customer, sales ) . All I need is to query this table and get an php array.

这是我的代码:

$query = "SELECT * FROM mysqltable"; $result = $mysqli->query($query))

我需要编写一个代码来循环并获取这种格式的数组:

I need to write a code to loop and get an array in this format:

$row=array(0=>"500,200",1=>"500,300",2=>"1000,600",3=>"800,400",4=>"200,500");

所以我可以以

echo json_encode($row);

非常感谢您帮助解决此问题.

Would appreciate any help to solve this.

推荐答案

可以使用mysqli_fetch_all()

You can use mysqli_fetch_all()

mixed mysqli_result::fetch_all ([ int $resulttype = MYSQLI_NUM ] )

mixed mysqli_fetch_all ( mysqli_result $result [, int $resulttype = MYSQLI_NUM ] )

参考阅读php/manual/en/mysqli-result.fetch-all.php

还有

<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3"; $result = $mysqli->query($query); while($row = $result->fetch_array()) { $rows[] = $row; } foreach($rows as $row) { echo $row['CountryCode']; } /* free result set */ $result->close(); /* close connection */ $mysqli->close(); ?>

更多推荐

从Mysql中获取数据并转换为PHP数组

本文发布于:2023-11-01 18:22:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   数据   Mysql   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!