Guest User

Untitled

a guest
Sep 13th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. Call to a member function prepare() on a non-object with PDO
  2. require_once("connect.php");
  3.  
  4. function get_username($uid){
  5. $stmt = $pdo->prepare("SELECT username FROM users WHERE uid= ?");
  6.  
  7. try {
  8. $stmt->execute(array($uid));
  9. } catch (PDOException $e) {
  10. echo $e -> getMessage(); exit;
  11. }
  12.  
  13. $row = $stmt->fetch();
  14. return($row['username']);
  15. }
  16.  
  17. $id = 1;
  18. echo get_username($id);
  19.  
  20. Call to a member function prepare() on a non-object
  21.  
  22. $dsn = 'mysql:dbname=test_db;host=127.0.0.1';
  23. $user = 'test_user';
  24. $password = 'test_pass';
  25.  
  26. try {
  27. $pdo = new PDO($dsn, $user, $password);
  28.  
  29. } catch (PDOException $e) {
  30. echo 'Connection failed: ' . $e->getMessage();
  31. exit;
  32. }
  33.  
  34. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  35.  
  36. error_reporting(E_ALL);
  37. session_start();
  38.  
  39. function get_username($uid){
  40. global $pdo;
  41. $stmt = $pdo->prepare("SELECT username FROM users WHERE uid= ?");
  42.  
  43. function get_username($uid, $pdo){
  44.  
  45. ...
  46. ...
  47. }
  48.  
  49. get_username( $id, $pdo);
Add Comment
Please, Sign In to add comment