0
how to retrieve data from mysql database to php variable
How can I retrieve data from a database to a variable with PHP?
2 ответов
+ 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.
0
Thanks a lot bro!