I am just a newbie in 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
$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 ? it's pretty useful. try using the mysqli_fetch_array(); or mysqli_fetch_assoc();
an example using your php code could be this:
<?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();
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.