Created
January 5, 2014 10:14
-
-
Save innbitcodes/8266526 to your computer and use it in GitHub Desktop.
Way to connect MySQL with PDO method in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| try{ | |
| $db = new PDO("mysql:host=HOST_NAME;dbname=DB_NAME;port=PORT_NUM", "USER", "PASS"); | |
| $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
| $db->exec("SET NAMES 'utf8'"); | |
| } catch (Exception $e) { | |
| echo "Could not connect to database."; | |
| exit; | |
| } | |
| try { | |
| $results = $db->query("SELECT .. FROM .. WHERE .."); | |
| } catch (Exception $e) { | |
| echo "Something wrong!"; | |
| } | |
| $results->fetchAll(PDO::FETCH_ASSOC); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment