0

if plan to create database class to use it in any php project , so i write the basic class with little code below , but when test it i got error message Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\cms\includes\cmsDatabase.php on line 38 Although i create connection in class constructor assigned to the local var.

My Code :

<?php
class cmsDatabase {


//Database Info 
var $db_host = "localhost";
var $db_username = "root";
var $db_password="root";
var $db_database= "mydb";

//Database Parameters 
var $database_connection ;
var     $database_db ;
var   $error_Message ;   

public function __construct(){
    $database_connection = mysqli_connect($this->db_host,$this->db_username,$this->db_password,$this->db_database) or die("can't connect to server") ;

}// end __construct  

public function __destruct(){
    mysqli_close($this->database_connection);
}// end __destruct   


function getLastError(){
    return $this->error_Message  ;
    }


//==================== DATABASE OPERATIONS ======================
function getConnection(){
        return $this->database_connection ;
}

function selectQuery ($sql){

    $result = mysqli_query($this->database_connection,$sql); //>> ERROR HERE
    return $result ;

}




}//Class 



?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
Realbitt
  • 388
  • 6
  • 18
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Oct 28 '13 at 13:48
  • it's different issue. – Realbitt Oct 28 '13 at 14:20

1 Answers1

3

You have to use:

$this->database_connection in your constructor, otherwise its just a local variable and out of scope for your query.

Update: Oh and don't use var, it's deprecated in PHP 5: What does PHP keyword 'var' do?

Community
  • 1
  • 1
puelo
  • 5,464
  • 2
  • 34
  • 62