Hej alle sammen jeg får disse fejl / advarsel når jeg kører min kode i PHP
Fejl:
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\ZinXan\core\class\DB.php on line 26
DB.php
- class DB {
- private static $_instance = null;
- private $_query, $_error, $_pdo;
- private $user = "root", $pass = "";
-
- private function __construct() {
- try {
- $this->_pdo = new PDO('mysql:host=127.0.0.1;', 'dbname=zinxan', $this->user, $this->pass);
- } catch (PDOException $e) {
- die($e->getMessage());
- }
- }
-
- public static function getInstance() {
- if (!isset(self::$_instance)) {
- self::$_instance = new DB();
- }
- return self::$_instance;
- }
-
- public function Query($sql, $params = array()) {
-
- $this->_error = false;
-
- if ($this->_query = $pdo->prepare($sql)) {
- $x = 1;
- if (count($params)) {
- foreach ($params as $param) {
- $this->_query->BindValue($x, $param);
- $x++;
- }
- }
-
- if($this->_query->execute()) {
- echo 'Success';
- }
- }
- }
- }
Når jeg skal kører min kode
- $x = "Mathias";
- $user = DB::getInstance()->Query("SELECT username FROM users WHERE username = ?", array($x));
- if ($user->error()) {
- echo 'No user';
- } else {
- echo 'User found';
- }
Tak på forhånd.
Indlæg senest redigeret d. 23.06.2014 18:16 af Bruger #17607