0

How to connect the database and fetch the data from database in php??

23rd Apr 2019, 2:00 PM
Dhan Bir Sunwar
Dhan Bir Sunwar - avatar
3 ответов
+ 2
To connect to MySQL database server, you need to create a new connectionobject with the data source name, user name and password. The connectionobject is an instance of the PDO class. If something went wrong while establishing a connection to theMySQL server, an error message will display. link: php.net/manual/en/pdo.connections.php To fetch data (and display) from database you can try this piece of code: <?php try { //dbconnection     $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);  foreach($dbh->query('SELECT * from `Tablename` ') as $row) { //print the result of an array expected on the screen         print_r($row);     }     $dbh = null; //this improve to handle errors and to help to resolving them } catch (PDOException $e) {     print "Error!: " . $e->getMessage() . "<br/>";     die(); } ?> ALSO REMEMBER THAT: - You can't submit forms on SoloLearn. - There is a limit to what you can do on SoloLearn. So...try XAMPP or other application and run php script on your pc.
23rd Apr 2019, 2:18 PM
Alessio Benvenuti
Alessio Benvenuti - avatar
+ 2
you're welcome
23rd Apr 2019, 3:35 PM
Alessio Benvenuti
Alessio Benvenuti - avatar
+ 1
Thabk you BOSS.
23rd Apr 2019, 3:08 PM
Dhan Bir Sunwar
Dhan Bir Sunwar - avatar