Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Call to a member function prepare() on a non-object with PDO
- require_once("connect.php");
- function get_username($uid){
- $stmt = $pdo->prepare("SELECT username FROM users WHERE uid= ?");
- try {
- $stmt->execute(array($uid));
- } catch (PDOException $e) {
- echo $e -> getMessage(); exit;
- }
- $row = $stmt->fetch();
- return($row['username']);
- }
- $id = 1;
- echo get_username($id);
- Call to a member function prepare() on a non-object
- $dsn = 'mysql:dbname=test_db;host=127.0.0.1';
- $user = 'test_user';
- $password = 'test_pass';
- try {
- $pdo = new PDO($dsn, $user, $password);
- } catch (PDOException $e) {
- echo 'Connection failed: ' . $e->getMessage();
- exit;
- }
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- error_reporting(E_ALL);
- session_start();
- function get_username($uid){
- global $pdo;
- $stmt = $pdo->prepare("SELECT username FROM users WHERE uid= ?");
- function get_username($uid, $pdo){
- ...
- ...
- }
- get_username( $id, $pdo);
Add Comment
Please, Sign In to add comment