Guest User

Untitled

a guest
Jun 30th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. <?php
  2. /*
  3. * The following function will create a PDO connection form PHP.
  4. * It sovles the issue of inserting the Unicode Characters.
  5. */
  6.  
  7. function connect_db() {
  8. $servername = "localhost";
  9. $db_username = "root";
  10. $db_password = "root";
  11. $db_name = "DATABASE_NAME";
  12.  
  13. try {
  14. $conn = new PDO("mysql:host=$servername;dbname=$db_name;charset=utf8", $db_username, $db_password);
  15. // set the PDO error mode to exception
  16. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  17. $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  18. $conn->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAME'utf8'");
  19.  
  20. echo "\nPDO connection object created\n";
  21.  
  22. return $conn;
  23. } catch (PDOException $e) {
  24. echo "Error: " . $e->getMessage();
  25. }
  26. }
Add Comment
Please, Sign In to add comment