Jump to content

Call to a member function query() on null


phreak3r

Recommended Posts

The code is a bit of a mess. I am trying to convert this procedural code to OO style. I have already done so in the dbcon/dbcon.php class, however, I am trying to get the database connected and working to retrieve information from the database. I am being given an "Call to a member function query() on null" error. Any help? I have sort of started converting the channel/channel.php class over to OO style. I am new to doing things in the object-oriented format, I have preferred procedural, but it will only make things easier in the future to start re-writing the codebase in an object oriented format. Thanks for the assistance!

 

Code for dbcon.php:

<?php

	define('HOST', 'localhost');
	define('USERNAME', 'root');
	define('PASSWORD', '1234');
	define('DATABASE_NAME', 'soapbox');

class databaseAccess {
	//mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

	public $conn;

	function __construct() {
		$this->connectToDatabase();
	}

	// connection to database
	function connectToDatabase() {
		//mysqli::select_db(DATABASE_NAME);
		$this->conn = new mysqli(HOST, USERNAME, PASSWORD, DATABASE_NAME);
	}

	/*if (!$conn) {
		die("Connection failed: " . mysqli_connect_error());
	} else {
		echo "Connection successful!";
	}

	if (!mysqli_select_db($conn, $database)) {
		echo " Database not selected!";
	} else {
		echo " Database selected!";
	}*/
}
?>

Code for channel.php:

<!-- 
	TODO:
	- Move elements to separate stylesheet
-->

<?php
include('../header.php');
require('../dbcon/dbcon.php');
include('../functions.php');

isLoggedIn();

$dbcon = new databaseAccess();
$conn = $dbcon->connectToDatabase();

$sql = "SELECT avatar, bio, account_open_date, user_id from profile0 WHERE username = '". $_SESSION['username'] . "' ";
$result = $conn->query($sql);
$row = mysqli_fetch_assoc($result);

$userID = $row['user_id'];

$url = "/soapbox/";
$avatar = $row['avatar'];
$bio = $row['bio'];
$join_date = date('F j, Y', strtotime($row['account_open_date']));

$username = $_SESSION['username'];
$sql = "SELECT video_id, thumbnail, video_title from videos0 WHERE uploader='$username'";
$result = mysqli_query($conn, $sql);
$num = mysqli_num_rows($result);
?>
Link to comment
Share on other sites

adding a database 'wrapper' class around another class (the mysqli class) is a waste of time (unless you are doing this as part of a typing class   :happy-04:  )

 

next, forget about the mysqli extension. you should be spending your time learning and using the much simpler and more consistent PDO extension, especially since you should be using prepared queries when supplying data values to the sql query statements, and the mysqli extension is overly complicated when doing prepared queries.

Link to comment
Share on other sites

adding a database 'wrapper' class around another class (the mysqli class) is a waste of time (unless you are doing this as part of a typing class   :happy-04:  )

 

next, forget about the mysqli extension. you should be spending your time learning and using the much simpler and more consistent PDO extension, especially since you should be using prepared queries when supplying data values to the sql query statements, and the mysqli extension is overly complicated when doing prepared queries.

 

Ah, well, I am still new to this. But, okay, I guess I will just start using and learning PDO. The answer isn't much help to me, but thanks?

Link to comment
Share on other sites

you are trying to convert procedural mysqli to OO mysqli, which will take a bunch of time. when you get around to using prepared queries, you will have to convert the code again, because the programming interface for non-prepared mysqli queries and prepared mysqli queries is completely different.

 

save yourself the time and just convert the code once, to use PDO. the programming interface for non-prepared and prepared PDO queries is the same. i recommend that you review your thread where you asked if you should switch to PDO.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.