Fatal Error: Call to a Member Function Query() on Null

Fatal error: Call to a member function query() on null

First, you declared $db outside the function. If you want to use it inside the function, you should put this at the begining of your function code:

global $db;

And I guess, when you wrote:

if($result->num_rows){
return (mysqli_result($query, 0) == 1) ? true : false;

what you really wanted was:

if ($result->num_rows==1) { return true; } else { return false; }

which is equivalent to:

return $result->num_rows == 1;

Fatal error: Call to a member function query() on null in

You tried to call query() on $con, but you haven't declared $con anywhere.

Fatal error: Call to a member function query() on null

Define $con:

 $con = mysqli_connect($host, $username) or die('Impossibile connettersi al server: ' . mysqli_error());

PHP Uncaught Error: Call to a member function query() on null

selectQuery() isn't a static method, it needs to be called through an object. You can use $this in the child class.

Also, the child class constructor needs to call the parent class's constructor so that the database connection will be opened.

And then you need to assign the result of selectQuery() to the $data variable so you can access the columns.

class Image extends Database
{
public $imageid;
public function __construct(int $imageid, int $userid)
{
parent::__construct();
$selectors = array(
"imageid" => $imageid,
"userid" => $userid,
);
$where = "images";
$data = $this->selectQuery($where, $selectors); //look here
$this->imagestring = $data['imagestring'];
$this->imagecomment = $data['imagecomment'];
$this->imagedate = $data['imagedate'];
$this->imageprivate = $data['private'];
$this->imageorder = $data['imageorder'];
$this->albumid = $data['albumid'];
}
//rest of image class
}

The child class shouldn't declare its own $con variable, it inherits that from the parent automatically.

Uncaught Error: Call to a member function query() on null

You are declaring instance of mysqli called $conn. This represents your connection to DB. You should call methods on variable $conn and not on (undefined) variable $mysqli. So ie. your line 19 should be:

$result = $conn->query("SELECT username FROM registration WHERE username = '$uname'")

Also to prevent SQL-Injection on your queries/web-pages you should use prepared statements EVERYWHERE(including SELECT).

PHP Uncaught Error: Call to a member function query() on null,

$conn variable is not accessible as global variable because you declare $conn as local variable inside connect() method.

To address this problem, you need to declare as class field and then you can access $conn as $this->conn inside any methods of class Index. This way you do not need global $conn.

For example:

<?php
class Index{
private $conn;

public function connect() {
...
$this->conn = $mysqli->select_db("webartisan");
...
}

public function insert_data() {
...
$result = $this->conn->query($query);
...
}
}

Update:

select_db() returns boolean value, not connection object. Following line will cause Call to a member function query() on boolean error as you said in comment.

$this->conn = $mysqli->select_db("webartisan");

You should read mysqli documentation.

Try/catch 'Call to a member function query() on null' error

The problem is you're catching an Exception, not an Error (that is being throw). If you want to catch both (exceptions and errors), you should use Throwable.

try
{
// Code that may throw an Exception or Error.
}
catch (Throwable $t)
{
// Executed only in PHP 7, will not match in PHP 5
}

As it says in doc:

As the Error hierarchy does not inherit from Exception, code that uses catch (Exception $e) { ... } blocks to handle uncaught exceptions in PHP 5 will find that these Errors are not caught by these blocks. Either a catch (Error $e) { ... } block or a set_exception_handler() handler is required.

Call to a member function query() on null in PHP

this->db->query function need only SQL Code. not like PDO.

Make sure Library is loaded

In config/autoload.php add this $autoload['libraries'] = array('database');


Try this

function fetch_url($url_code) {

$query = $this->db->query("SELECT * FROM urls WHERE url_code = '$url_code' ");
$result = $query->result_array(); # setting array to objective
$count = count($result); # get count

if (!empty($count)) {
return $result;
}
else {
return false;
}
}

Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\shop\index.php

You forgot to create PDO connection:

<?php 
function getDB(){
static $db;
if($db instanceof PDO){
return $db;
}
require_once CONFIG_DIR.'/database.php';
$dsn = sprintf("myqsl:host=%s;dbname=%s;charset=%s",DB_HOST,DB_DATABASE,DB_CHARSET);
$db = new PDO($dsn); // ← HERE
return $db;
}


Related Topics



Leave a reply



Submit