Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. <?php
  2. class Connection {
  3. public $connection;
  4. public $dbHost = 'localhost';
  5. public $dbName = 'employees';
  6. public $charset = 'charset=utf8';
  7. public $dbUser = 'root';
  8. public $dbPassword = '';
  9.  
  10. public function __construct() {
  11. try {
  12. $this->connection = new PDO ("mysql:host=$this->dbHost;$this->dbName;$this->dbUser;
  13. $this->dbPassword");
  14. $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15.  
  16. }
  17. catch (PDOException $e) {
  18. echo "There is something wrong with the database".$e->getMessage();
  19. }
  20. }
  21. function insertUserValues($tableName, $data) {
  22. $query = "INSERT INTO ".$tableName."(";
  23. $query .= implode (",",array_keys($data)).') VALUES (';
  24. $query .= "'" . implode ("','",array_values($data))."')";
  25. }
  26. }
  27. $users = new Connection();
  28. ?>
  29.  
  30. <?php
  31. $dbhost = "localhost";
  32. $dbname = "pdo";
  33. $dbusername = "root";
  34. $dbpassword = "845625";
  35.  
  36. $link = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword);
  37.  
  38. $statement = $link->prepare("INSERT INTO testtable(name, lastname, age)
  39. VALUES(:fname, :sname, :age)");
  40. $statement->execute(array(
  41. "fname" => "Bob",
  42. "sname" => "Desaunois",
  43. "age" => "18"
  44. ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement