Fatal error: Cannot use object of type DBConnect as array in C:\xampp\htdocs\class.DBConnect.php on line 28
class DBConnect
{
// access is private for all class variables
private $c_host_name;
private $c_user_name;
private $c_user_password;
private $c_user_database;
private $c_db_link;
private $c_arr_errors;
private $c_obj_prepared_query_statement;
// class constructor
public function __construct($p_arr_connection_string)
{
$this->c_host_name = $p_arr_connection_string['host_name'];
$this->c_user_name = $p_arr_connection_string['user_name'];
$this->c_user_password = $p_arr_connection_string['user_password'];
$this->c_user_database = $p_arr_connection_string['user_database'];
$this->c_arr_errors = '';
if ($this->validate_connection_string())
$this->connect_to_database();
}
// connect to the required database
// generate error messages on error
private function validate_connection_string()
{
$m_validated = true;
// validate passed in values
if (empty($this->c_host_name) || empty($this->c_user_name)
|| empty($this->c_user_password) || empty($this->c_user_database))
{
$m_validated = false;
$this->c_arr_errors['db_connect'] = 'Invalid database connection values';
}
return $m_validated;
}
// attempt to connect to database server & specified database
private function connect_to_database()
{
try
{
//$m_connect_string = $this->c_host_name . ';dbname=' . $this->c_user_database;
$this->c_db_link = new PDO($this->c_host_name, $this->c_user_database, $this->c_user_name, $this->c_user_password);
}
catch (PDOException $p_error)
{
$this->c_arr_errors['db_connect'] = $p_error->getMessage();
}
}
// insert escape character to POSTED data strings for database access
public function escape_string($p_string_to_escape)
{
return $this->c_db_link->real_escape_string($p_string_to_escape);
}
// close the database link
public function return_errors()
{
$m_return = false;
if ($this->c_arr_errors != '')
$m_return = $this->c_arr_errors;
return $m_return;
}
// prepare a query
public function prepare_query($p_query_to_prepare)
{
$this->c_obj_prepared_query_statement = $this->c_db_link->prepare($p_query_to_prepare);
}
// bind the parameters to the prepared query
public function bind_to_prepared_query($p_parameters)
{
$m_arr_temp = array(); // hack to correct foreach array reading 'feature'
foreach ($p_parameters as $m_key => $m_value)
{
$m_arr_temp[$m_key] = $m_value;
if (!$this->c_obj_prepared_query_statement->bindParam($m_key, $m_arr_temp[$m_key]))
$this->c_arr_errors['binding error'] =$this->c_obj_prepared_query_statement->errorInfo();
}
}
// execute the prepared query
public function execute_prepared_query()
{
if (!$this->c_obj_prepared_query_statement->execute())
print_r ($this->c_obj_prepared_query_statement->errorInfo());
}
public function query_fetch_assoc_array()
{
$m_query_result = $this->c_obj_prepared_query_statement->fetch(PDO::FETCH_ASSOC);
return $m_query_result;
}
public function query_fetch_index_array()
{
$m_query_result = $this->c_obj_prepared_query_statement->fetch(PDO::FETCH_NUM);
return $m_query_result;
}
public function count_rows()
{
$m_number_of_rows_returned = $this->c_obj_prepared_query_statement->rowCount();
return $m_number_of_rows_returned;
}
// close the database link
public function close_db_link()
{
$this->c_db_link = null;
}
} // end of class DBConnect
?>
What array is the error refering to?
Is the connect_to_database function correct?