SHARE
TWEET

Untitled

a guest Feb 4th, 2016 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. echo '<pre>';
  4.  
  5. /*
  6. Have a table like
  7.  
  8. DROP TABLE `users`;
  9. CREATE TABLE `users` (
  10.   `id` int unsigned NOT NULL AUTO_INCREMENT,
  11.   `name` varchar(100) NOT NULL,
  12.   `l` mediumint(8) unsigned DEFAULT NULL,
  13.   `r` mediumint(8) unsigned DEFAULT NULL,
  14.   PRIMARY KEY (`id`)
  15. );
  16.  
  17. insert into `users` VALUES (1, "Joe", 1, 2),(2, "Jane", 3, 4);
  18.  
  19.  */
  20.  
  21. // fill in as needed
  22. $host = 'xxx.xxx.xxx.xxx';
  23. $user = 'cooldude';
  24. $pass = 'awesomepasswordman';
  25. $db   = 'yourdatabase';
  26.  
  27. ////////////////////////////////////////////
  28.  
  29. echo "Unprepared queries\n";
  30.  
  31. $m = new mysqli($host, $user, $pass, $db);
  32. print_r(
  33.     $v = $m->query('SELECT `name`, `id`, `l`, `r` FROM users WHERE id = 1')->fetch_assoc()
  34. );
  35.  
  36. $pdo = new PDO("mysql:dbname=$db;host=$host;port=3306", $user, $pass);
  37. print_r(
  38.     $pdo->query('SELECT `name`, `id`, `l`, `r` FROM users WHERE id = 1')->fetchAll(PDO::FETCH_ASSOC)
  39. );
  40.  
  41. ////////////////////////////////////////////
  42.  
  43. echo "Prepared queries\n";
  44.  
  45. $m = new mysqli($host, $user, $pass, $db);
  46. $stmt = $m->prepare('SELECT `name`, `id`, `l`, `r` FROM users WHERE id = ?');
  47. $id = 1;
  48. $stmt->bind_param("i", $id);
  49. $stmt->execute();
  50. $result = $stmt->get_result()->fetch_assoc();
  51. print_r(
  52.     $result
  53. );
  54.  
  55. $pdo = new PDO("mysql:dbname=$db;host=$host;port=3306", $user, $pass, [
  56.     PDO::ATTR_EMULATE_PREPARES => false,
  57. ]);
  58. $stmt = $pdo->prepare('SELECT `name`, `id`, `l`, `r` FROM users WHERE id = ?');
  59. $stmt->execute([1]);
  60. print_r(
  61.      $stmt->fetchAll(PDO::FETCH_ASSOC)
  62. );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top