phreak3r Posted February 24, 2018 Share Posted February 24, 2018 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/306637-call-to-a-member-function-query-on-null/ Share on other sites More sharing options...
mac_gyver Posted February 24, 2018 Share Posted February 24, 2018 (edited) 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 ) 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 February 24, 2018 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/306637-call-to-a-member-function-query-on-null/#findComment-1556679 Share on other sites More sharing options...
phreak3r Posted February 24, 2018 Author Share Posted February 24, 2018 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 ) 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? Quote Link to comment https://forums.phpfreaks.com/topic/306637-call-to-a-member-function-query-on-null/#findComment-1556682 Share on other sites More sharing options...
Solution mac_gyver Posted February 24, 2018 Solution Share Posted February 24, 2018 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/306637-call-to-a-member-function-query-on-null/#findComment-1556683 Share on other sites More sharing options...
phreak3r Posted February 24, 2018 Author Share Posted February 24, 2018 Okay, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/306637-call-to-a-member-function-query-on-null/#findComment-1556686 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.