0

how to retrieve data from mysql database to php variable

How can I retrieve data from a database to a variable with PHP?

19th Dec 2016, 2:45 PM
Twaha Rahman
Twaha Rahman - avatar
2 odpowiedzi
+ 1
To connect to a database: try{ $db = new PDO('mysql:host=YOUR_HOST_HERE(if locally hen you shoud write "Localhost" or "127.0.0.1");dbname=YOUR_DATABASE_NAME;charset=utf8', 'YOUR_USERNAME', 'YOUR_PASSWORD', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); }catch(exception $e) { die('Could not connect to database'.$e->getMessage()); } To retrieve data : $data = $db->query('SELECT * FROM table_name'); $myData = $data->fetch(PDO::FETCH_ASSOC); //or PDO::FETCH_OBJ if you like to deal with objects. And here you go, now you have $myData variable which is an array that contains all what's in your database.
19th Dec 2016, 2:57 PM
CHMD
CHMD - avatar
0
Thanks a lot bro!
20th Dec 2016, 12:23 AM
Twaha Rahman
Twaha Rahman - avatar