Jump to content

Call to a member function query() on null


Go to solution Solved by mac_gyver,

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
https://forums.phpfreaks.com/topic/306637-call-to-a-member-function-query-on-null/
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.

Edited by mac_gyver

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?

  • Solution

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.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.