php - Prepared statement not sending

854

I am having an issue with a framework I have used quite often for different sites. I copy and pasted the framework from a working website I have and changed nothing other than my database information, but for some reason I am getting multiple errors associated with my database file. I am including partial files to show the relevant code from my database file and my init file.

Does anyone see anything I am doing wrong?

I am getting errors for the following lines:

Line 13) Notice: Array to string conversion and Warning: PDO::__construct() expects parameter 2 to be string$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));

My debug calls out this:

DEBUG DB::query called SQL: SELECT * FROM users WHERE username = ?

PARAMS: test

Then on line 33,

It gives this error:

Fatal error: Call to a member function prepare() on a non-object in

For this line:if($this->_query = $this->_pdo->prepare($sql)) {

Database class

class DB {
    private static $_instance = null;
    private $_pdo,
            $_query, 
            $_error = false,
            $_results,
            $_count = 0,
            $_errmsg = "";

    private function __construct(){
        try {
            $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
            /*$host = config::get('mysql/host');
            $database = config::get('mysql/db');
            $username = config::get('mysql/user');
            $pasword = config::get('mysql/password');

            $dbh = new PDO('mysql:host=$host;dbname=$database', $username, $pasword);*/
            } 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()){
echo "DEBUG DB::query called<br>SQL: $sql<br><br>PARAMS: " . implode("<br>", $params) . "<hr>\n";       
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql)) {
echo "DEBUG: prepared statement created ok<hr>\n";          
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }
            if($this->_query->execute()){
                $this->_results = $this->_query->fetchALL(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
echo "DEBUG: query succeeded, rowcount was: " . $this->_count . "<hr>\n";               
            } else {
    "DEBUG: query failed to execute, reason:<br>" . implode( "<br>", $this->_query->errorInfo() ) . "<hr>\n";   
                $this->_error = true;
            }
        } else {
echo "DEBUG: Failed to create prepared statement<hr>\n";
        }   
        return $this;
    }

Init class

$GLOBALS['config'] = array(
    'mysqli' => array(
        'host' => 'localhost',
        'username' => 'actual',
        'password' => 'actual',
        'db' => 'actual'
    ),
    'remember' => array(
        'cookie_name' => 'hash',
        'cookie_expiry' => 604800
    ),
    'session' => array(
        'session_name' => 'user',
        'token_name' => 'token'
        )
    );

    spl_autoload_register(function($class) {
        require_once 'classes/' . $class . '.php';
    });

    require_once 'functions/sanitize.php';

People are also looking for solutions to the problem: javascript - Why jQuery $('#form').submit(); works perfectly BUT Not $('#form').submit(function(){...});?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.