convert mysql db to php array in particular format -
i have seen other solutions don't put data in php array way described below.
i have database called: users
in database there many tables e.g. alice,bob
each table has following columns e.g. id,name,age
i data , put php array, so:
array ( [db_users] => array ( [0] => array ( [id] => 1 [name] => alice [age] => 34 ) [1] => array ( [id] => 2 [name] => bob [age] => 42 ) ) )
you should have 1 table called users
columns id
, name
, , age
alice, bob, et. al. 1 row in table. have 1 simple query return array desire.
select * users
here's example schema mysql
create table users ( id int primary key auto_increment, name varchar(255), age int ); insert users (name, age) values ('alice', 34), ('bob', 42);
and here's example using pdo
<?php // returns intance of pdo // https://github.com/jpuck/qdbp $pdo = require __dir__.'/example_dhbxcw_a.pdo.php'; $sql = 'select * users'; $array = $pdo->query($sql)->fetchall(pdo::fetch_assoc); print_r($array);
results in output
array ( [0] => array ( [id] => 1 [name] => alice [age] => 34 ) [1] => array ( [id] => 2 [name] => bob [age] => 42 ) )
Comments
Post a Comment