Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

PDO Tutorial Connect Database and Query Example

Learn how to connect to a mysql database and query it using the PDO extension in PHP. After this PDO coding exercise we conduct a mysqli vs PDO discussion that you may find informative. <?php try { $db = new PDO('mysql:host=localhost;dbname=db_name', 'db_user', 'db_password'); } catch (PDOException $e) { echo $e->getMessage()."<br>"; die(); } $sql = 'SELECT username, country from people'; foreach( $db->query($sql) as $row ) { echo $row['username']." - ".$row['country']."<br>"; } $db = null; ?> Link to document we discussed about the PHP mysqli extension. http://php.net/manual/en/mysqli.overview.php
thumb

PHP Videos

Miscellaneous

Website Design Theme Application PHP Cookies CSS TutorialPDO Prepared Statements PHP TutorialPDO Tutorial Connect Database and Query ExampleActive Page Link Button Highlight TutorialGeoPlugin Tutorial Get User Location Information IP DetectionHashtag System and Regex PHP Programming TutorialPagination MySQLi Google Style Paged Results TutorialActive Clickable Link Function preg_replace Programming TutorialGet Favicon From Web Site Link Access External FilesRandom String Generator PHP Function Programming TutorialForce File Download Dialog In Browser TutorialCheck User Sign Up Name Ajax PHP Social Network TutorialFlat File Text Database System CMS Edit Content TutorialFlat File Text Database System Display ContentExperience Level Evaluation Programming TutorialStagger and Limit Dynamic Output in a While Loop Google AdsTwitter Dynamic Goodies Widget TutorialDynamic Google Maps Location TutorialPrepared Statements PHP mysqli Tutorial