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?

public function __construct($p_arr_connection_string)
		{
			$this->c_host_name = $p_arr_connection_string['host_name'];  //this is line 28
			$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();
		}

which part is the array the error is refering to?

is $p_arr_connection_string an object?

<?php
  $host_name = 'locahost';
  $user_name = 'root';
  $user_password = 'root';
  $user_database = 'joop';
 ?>

connect.php

$p_arr_connection_string is a passed parameter which uses
require_ once connect.php

can you show me how its called? for some reason I am confused. It sounds like you are using $obj = new DBConnect(require_once('connect.php'));

require_once ("connect.php");
require_once ("class.DBConnect.php");
require_once ("class.Page.php");
require_once ("class.registerController.php");

// connect to database server 
$obj_db_link = new DBConnect($arr_connection_string);
$obj_register = new registerController($obj_db_link);
$name = $_POST['name_txt'];
$email = $_POST['email_txt'];
$username = $_POST['username_txt'];
$password = $_POST['password'];

rgr.php

class registerController
{
    private $c_db_link;
    private $c_arr_errors;
    
public function __construct($p_arr_connection_string)
{
$this->c_arr_errors = array();
require_once 'DBConnect.php';
$this->c_db_link = new DBConnect($p_arr_connection_string);
$m_error = $this->c_db_link->return_errors();
if ($m_error)
$this->c_arr_errors = $m_error['db_connect'];
}

public function __destruct()
{
// clean up -- close the database handle
$this->c_db_link->close_db_link();
}
public function register_account()
{
// store an SQL query 
$m_register_query = "INSERT INTO users values('$username','$password','$name','$email')";
// execute the SQL query
$m_result = $this->c_db_link->query($m_register_query);
return $m_result;

}

rgr.class.php

once data is posted, rgr makes a connection to database.

right here:

$obj_register = new registerController($obj_db_link);

is your problem. you are passing an object, not the connection string. the __construct is trying to interpret that as an array and its throwing an error.
Change that to $obj_register = new registerController($arr_connection_string);

I tried the above the above, I keep getting this error
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\class.DBConnect.php on line 84
??

public function prepare_query($p_query_to_prepare)
        {
            $this->c_obj_prepared_query_statement = $this->c_db_link->prepare($p_query_to_prepare);
        }

make sure there is a prepare function in your DBConnect class and make sure that c_db_link is an actually object.

make sure there is a prepare function in your DBConnect class and make sure that c_db_link is an actually object.

thats the problem, the c_db_link is an instance of the class.DBconnect.php (database class- which uses PDO)

i read that PDO cannot be an object but a resource pointer?

there is a prepare function in my DBconnect $this->c_obj_prepared_query_statement = $this->c_db_link->prepare($p_query_to_prepare);

how can i test if there is actually a connection to the mysql server using this dbconnect (which uses pdo)

I have found that PDO connection doesnt work.

catch (PDOException $p_error)
			{
				echo $this->c_arr_errors['db_connect'] = $p_error->getMessage();
			}

i get the error using the above

"Invalid database connection values"

somehow the database values (name,username,password etc.) are not being read properly.

my database connection values are in mysql_connect.php

Any ideas?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.