Problem displaying data of database using class

Hello i’m having problem in my php class, when i tried to return the data it outputs nothing.
Heres my code:

class Connection
	{
		public $connection;

		function __construct($hostname,$username,$password,$database)
		{
			# code...
			$this->hostname = $hostname;
			$this->username = $username;
			$this->password = $password;
			$this->database = $database;
			
		}

		function setConnection(){
			$this->connection = new mysqli($this->hostname,$this->username,$this->password,$this->database);
			if(!$this->connection) die('connection problem');
			return $this->connection;
		}
	}

//data class
    class Data
{
	public $datalist;
	function __construct(Connection $connection)
	{
		# code...
		$this->connection = $connection->setConnection();
	}

	function dataGrid()
	{
		$query = $this->connection->query('SELECT * FROM products');
		if($query->num_rows > 0){
			while(($this->datalist = $query->fetch())){
				return $this->datalist;
			}
		}
	}
}

i tried to call this in my index php using this but it outputs nothing:

$dbconnection = new Connection('localhost','root','','inventory');
$dbconnection->setConnection();
$dataresult = new Data($dbconnection);
$dataresult->dataGrid();
echo $dataresult->data_list['product'];

please anyone help me solve this, im trying to make my code maintainable so i use class, its my first time using oop so i don’t really get it much.

Some of the main points of user written classes/functions are that they should -

  1. Do something useful and add value over what the language can do itself.
  2. They should be general-purpose and be reusable.
  3. They should help you to write applications, by simplifying the main code.

If the code you have written doesn’t accomplish these things, than all it has done is add an unnecessary layer and you might as well have not used classes/functions for a particular task.

This code also contains a number of misunderstandings -

  1. An object will always be returned by new mysqli() (if the mysqli extension is installed), so, the posted connection error handling logic won’t ever be triggered. You can find how you would test for a mysqli OOP connection error in the php.net documentation. However, forget about having discrete logic handling connection errors in your code. Instead, use exceptions for all the database statement errors (connection, query, prepare, and execute) and in most cases let php catch the exception where it will use its error related settings to control what happens with the actual error information (database statement errors will ‘automatically’ get displayed/logged the same as php errors.) The exception to this rule is when inserting/updating user submitted data. In this case, your code should catch the exception, detect if a duplicate key error occurred, then set up a message telling the user what was wrong with the data they submitted.
  2. The setConnection() method returns the connection and you should do something with the return value in the calling code. Your main code is not doing anything with the return value and the repetitive call to this method inside the Data class, shouldn’t calling this method at all. Your main code is responsible for making the database connection, then it should use dependency injection to supply it to any class that needs it.
  3. The dataGrid() method is misnamed, for a couple of reasons. It is fetching a row of data and should be named as such, and by hard-coding the sql query inside of it, it’s specific to product(s) data, and again should be named as to its ‘product’ specific purpose. This method is not general purpose. You should not find yourself writing new classes/methods for every different database table or every different sql query.
  4. By RETURNing inside of the dataGrid() while(){} loop, only the first row of data will get fetched. If you have a query that will match at most only one row, don’t use a loop to fetch that data and for a query that can match a set of one or more rows, fetch all the data into an array, then return that array.
  5. The dataGrid() method is (trying to) return data, but the main code is not using this and is instead trying to reference a ‘data_list’ properly that doesn’t exist, i.e. ‘data_list’ is not the same as ‘datalist’.

Trying to convert procedural code to be OOP, is more than just adding class definitions around parts of the code. You need to define what the methods and properties are for each different type of object, then write the class code to match.

BTW - you should switch to the PDO extension. It is much simpler and more consistent than the mysqli extension.

Based on some of the problems in the code, do you have php’s error_reporting set to E_ALL and display_errors set to ON, so that php would help you?

1 Like

Mostly what phdr stated.

$dbconnection = new Connection('localhost','root','','inventory');
$conn = $dbconnection->setConnection();
$dataresult = new Data($conn);
print_r($dataresult->dataGrid());

What does this display? A single record or nothing?

Sorry sir/mam for my late reply, Thank you very much! it really helps me a lot. i will try your suggestion of using PDO and yes i set my reporting and display_errors to and this is the error i got “Trying to access array offset on value of type null”.

Your suggestion and recommendation is really helpful thank you so much.

In my code i tried to display my data in product table in my mysql database. and when i tried to use print_r($dataresult->dataGrid()); it display nothing. but when i tried to echo the dataresult->datalist[‘product_id’]; i got this error “Trying to access array offset on value of type null”.

Displays nothing as in a white screen or nothing as in no objects?

White screen sir, no object displayed or any information or error.

function setConnection(){ 
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $this->connection = new mysqli($this->hostname,$this->username,$this->password,$this->database); 
    if(!$this->connection) die('connection problem'); 
    return $this->connection;
}
Sponsor our Newsletter | Privacy Policy | Terms of Service