[英]php fetch data from the database in an array format
I am just a newbie in php. 我只是php的新手。 I have a database, in the database the data is like this 我有一个数据库,数据库中的数据是这样的
CREATE TABLE IF NOT EXISTS `list` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
INSERT INTO `list` (`id`, `data`) VALUES
(1, '5,2,3,4,1');
Now to fetch the data I have made my php code like this 现在要获取数据,我已经将我的php代码变成了这样
<?php
$host = 'localhost';
$username = 'root';
$password = '';
$db = 'sortable';
$link=mysqli_connect($host, $username, $password, $db);
if (mysqli_connect_errno($link)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT `data` FROM `list`";
$order = mysqli_query($link, $query);
print_r($order);
?>
here its showing the result like this 在这里显示这样的结果
mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 1 [type] => 0 )
But I want the fetch values will be in array and the desired output of the array will be like this 但我希望获取值将在数组中,并且所需的数组输出将如下所示
Array([0] => Array([0]=>5,2,3,4,1[data]=>5,2,3,4,1))
So can someone kindly tell me how to do this? 那么有人可以告诉我该怎么做吗? Any help and suggestions will be really appreciable. 任何帮助和建议都是非常可取的。
have you tried looking up the mysqli_* commands in the php database found HERE ? 您是否尝试过在此处找到的php数据库中查找mysqli_ *命令? it's pretty useful. 这非常有用。 try using the mysqli_fetch_array(); 尝试使用mysqli_fetch_array(); or mysqli_fetch_assoc(); 或mysqli_fetch_assoc();
an example using your php code could be this: 使用您的php代码的示例可能是这样的:
<?php
$host = 'localhost';
$username = 'root';
$password = '';
$db = 'sortable';
$link=mysqli_connect($host, $username, $password, $db);
if (mysqli_connect_errno($link)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT `data` FROM `list` WHERE `id` = '1'";
$result = mysqli_query($link, $query);
$data = mysqli_fetch_array($result);
$numbers = $data['data'];
echo $numbers;
?>
For this Output 对于此输出
Array([0] => Array([0]=>5,2,3,4,1)
**Use this**
<?php
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DBNAME', 'sortable');
$db = new mysqli(HOST, USER, PASS, DBNAME);
if ($db->connect_errno) {
echo "Failed to connect to MySQL: ("
. $db->connect_errno . ") " . $db->connect_error;
}
$sql = "SELECT `data` FROM `list` WHERE `id` = '1'";
$result_db = $db->query($sql) or die("Error!");
$all_result = $result_db->fetch_all();
print_r($all_result);
$db->close();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.