Php call to member function query() on a non-object error - php

I'm using mysqli for a function, and I'm getting an error, Fatal error: Call to a member function query() on a non-object in /home/u250000297/public_html/forum/system/db.php on line 46 I've tried different things, but I just get different errors, what am I doing wrong and where is my error?
Code lines 45-51:
function fetch($query) {
$sql = $mysqli->query($query);
$result = $sql->fetch_array(MYSQLI_BOTH);
return $result;
$sql->free();
}
Here's my previous attempt and error : Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/u250000297/public_html/forum/system/db.php on line 46 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u250000297/public_html/forum/system/db.php on line 47 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/u250000297/public_html/forum/system/db.php on line 46 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u250000297/public_html/forum/system/db.php on line 47
Code, lines 45-51:
function fetch($query) {
$sql = mysqli_query($mysqli, $query);
$row = mysqli_fetch_array($sql, MYSQLI_BOTH);
return $row;
mysqli_free_result($row);
}
Connection:
$mysqli = mysqli_connect($this->host,$this->username,$this->password);
mysqli_select_db( $mysqli,$this->database );
if ($mysqli->connect_error) {
trigger_error('Database connection failed: ' . $mysqli->connect_error, E_USER_ERROR);
}

Either you need to define $mysqli as global or pass it as an argument.
After a return, no statement will be executed (mysqli_free).
Global:
function fetch($query) {
global $mysqli;
$sql = $mysqli->query($query);
$result = $sql->fetch_array(MYSQLI_BOTH);
return $result;
}
Parameter (and even better):
function fetch($myslqi, $query) {
$sql = $mysqli->query($query);
$result = $sql->fetch_array(MYSQLI_BOTH);
return $result;
}

Points to debug are
1) Is the connection to the database getting established?
2) Are your query strings correct?
Call to a member function query() on a non-object and Warning: mysqli_query() expects parameter 1 to be mysqli means that the $mysqli is null or isn't initialized properly. To get more information check the mysql error logs.

Related

Query returns results but Warning about boolean given [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 1 year ago.
I am trying to upgrade an old application to PHP 7.2. It contains an sql class PHP file with the following function which I have modified to use mysqli:
function query($query, $index=0)
{
// query
if (!$this->res[$index] = mysqli_query($this->connection, $query))
{
// if query fails show error
$this->error('<strong>invalid query</strong>:<br />' . $query . '<br />');
return false;
}
// statistical information
$this->num_rows[$index] = #mysqli_num_rows($this->res[$index]);
$this->num_flds[$index] = #mysqli_num_fields($this->res[$index]);
$this->num_aff[$index] = #mysqli_affected_rows($this->connection);
$this->last_id = #mysqli_insert_id($this->connection);
return true;
}
This function throws the folling error:
E_WARNING Error in file �sql.class.php� at line 132: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given E_WARNING Error in file �sql.class.php� at line 133: mysqli_num_fields() expects parameter 1 to be mysqli_result, boolean given
My initial thought was that the query was failing. However, including this line inside the function...
print_r(mysqli_fetch_assoc($this->res[$index]));
results in the following output:
Array ( [s_id] => 2088b4cc0d026c2742e8e0cb7d7c8e95 )
In the output above, the query is returning a session ID. That leaves me a bit confused because the value of $this->res[$index] is not a boolean, yet the Warning says it is.
Edit:
If I include this in the function:
echo mysqli_num_rows($this->res[$index]);
echo mysqli_num_fields($this->res[$index]);
Each line echos the correct value of 1 but each line also produces the boolean Warning...
E_WARNING Error in file �sql.class.php� at line 125: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given
E_WARNING Error in file �sql.class.php� at line 126: mysqli_num_fields() expects parameter 1 to be mysqli_result, boolean given
mysqli_query return values : If can be mysqli_result object,true, false
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
try this modification to see which query really successful but result was true
function query($query, $index=0)
{
$this->res[$index] = mysqli_query($this->connection, $query);
// query
if (is_bool($this->res[$index])) {
if ($this->res[$index] === false)
{
// if query fails show error
$this->error('<strong>invalid query</strong>:<br />' . $query . '<br />');
return false;
} else {
// query was successful, but the result is not a mysqli_result object
$this->warning('<strong>success with no returned data query</strong>:<br />' . $query . '<br />');
return true;
}
}
// statistical information
$this->num_rows[$index] = #mysqli_num_rows($this->res[$index]);
$this->num_flds[$index] = #mysqli_num_fields($this->res[$index]);
$this->num_aff[$index] = #mysqli_affected_rows($this->connection);
$this->last_id = #mysqli_insert_id($this->connection);
return true;
}
Check that $this->warning is exist in your code, or update it to what it will be proper

Php error when using mysqli

I've decided to using mysqli instead of mysql and I've having some errors, this is my first time using mysqli and I don't know what the errors are, any suggestions?
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/u250000297/public_html/forum/system/db.php on line 45
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u250000297/public_html/forum/system/db.php on line 45
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/u250000297/public_html/forum/system/db.php on line 45
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u250000297/public_html/forum/system/db.php on line 45
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /home/u250000297/public_html/forum/system/db.php on line 33
Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/u250000297/public_html/forum/system/db.php on line 33
Line 32-36
function query($query) {
$sql = mysqli_query($query, $this->db) or die(mysqli_error());
return $sql;
mysqli_free_result($sql);
}
Line 44-48
function fetch($query) {
$sql = mysqli_fetch_array(mysql_query($query, $this->db));
return $sql;
mysqli_free_result($sql);
}
Try this, You have used mysql_query instead of mysqli_query
function query($query) {
$sql = mysqli_query($this->db, $query) or die(mysqli_error());
...
}
function fetch($query) {
mysqli_fetch_array(mysqli_query($this->db, $query));
....
}
instead of
function fetch($query) {
mysqli_fetch_array(mysql_query($query, $this->db));
...
}
Try using a mysqli_query() function like this maybe
$data = mysqli_query($dbc, $query);
mysqli_fetch_array($data)
Notice that with mysqli_query() two arguments are passed:
the database connection variable
the query variable
Then use the the result of the mysqli_query() function as the argument to mysqli_fetch_array() function :D

Function getResult call

I'm working transferring my functions from MySQL to MySQLi standards.
This is my old function for getresult
function getResult($sql) {
$result = mysql_query($sql, $this->conn);
if ($result) {
return $result;
} else {
die("SQL Retrieve Error: " . mysql_error());
}
}
However, I have been following the W3schools on the mysqli_query function. Here's where I'm presently at.
function getResult($connection){
$result = mysqli_query($connection->conn);
if ($result) {
return $result;
} else {
die("SQL Retrieve Error: " . mysqli_error());
}
}
Now, the examples on W3schools are just a bit different from how I want to use mysqli_query. I'm trying to make it be directed at my DB, not some input or constants as per examples on W3S.
Notice: Trying to get property of non-object in C:\xampp\htdocs\cad\func.php on line 92
Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\cad\func.php on line 92
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\cad\func.php on line 96
SQL Retrieve Error:
Thank you
Your version won't work for a number of reasons, not the least of which is that you haven't included the query you want to send. Assuming $this->conn is set up properly somehow, try this:
function getResult($sql) {
$result = mysqli_query($this->conn, $sql); //Note parameters reversed here.
if ($result) {
return $result;
} else {
die("SQL Retrieve Error: " . mysql_error());
}
}
From the PHP help files I found these function definitions
mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )
string mysqli_error ( mysqli $link )
mysqli_query() takes the link and the actual query as mandatory parameters, and mysqli_error takes the link as one as well.
http://php.net/manual/en/mysqli.query.php
http://www.php.net/manual/en/mysqli.error.php

PHP error: Warning: ociparse() parameter 1 to be resource

As there is not ready made function in oracle to validate the Query so created.
So I tried below code to check whether the QUERY is valid or not.
if(isset($_POST['btn_Submit_Query']))
{
$check_query=$_POST['txtQuery'];
echo $check_query;
$valid = false;
$stmt = oci_parse($DB, $check_query);
echo "Statement" . $stmt;
//oci_define_by_name($stmt, 'NUMBER_OF_ROWS', $number_of_rows);
oci_execute($stmt, OCI_DEFAULT);
echo oci_num_rows($stmt);
}
I got following Warnings in the Execution:
Warning: oci_parse() expects parameter 1 to be resource, object given in D:\xampp\htdocs\app\DashBoardSite\Admin\querybuilder.php on line 899
Statement
Warning: oci_execute() expects parameter 1 to be resource, null given in D:\xampp\htdocs\app\DashBoardSite\Admin\querybuilder.php on line 902
Warning: oci_num_rows() expects parameter 1 to be resource, null given in D:\xampp\htdocs\app\DashBoardSite\Admin\querybuilder.php on line 903
Where is my mistake?
You must first connect to the database. This connection must a "resource" to variable $DB.

mysqli prepared expect parameter?

I try to write own database class.I have some problem.
When I run it , it gives these errors.
Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /Users/emrecetin/www/ayrox/m/db.php on line 75
Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in /Users/emrecetin/www/ayrox/m/db.php on line 76
Warning: mysqli_stmt_bind_result() expects parameter 1 to be mysqli_stmt, boolean given in /Users/emrecetin/www/ayrox/m/db.php on line 77
Warning: mysqli_stmt_fetch() expects parameter 1 to be mysqli_stmt, boolean given in /Users/emrecetin/www/ayrox/m/db.php on line 78
on these line;
mysqli_stmt_bind_param($sor, 's', $hede);
mysqli_stmt_execute($sor);
mysqli_stmt_bind_result($sor, $bas);
mysqli_stmt_fetch($sor);
I will give short version of databse class.It is not exactly, I write just what I use.
class db{
public $durum;
protected $server = 'localhost';
protected $suser = 'root';
protected $spass = 'root';
public $db = 'members';
public $durum; // mysqli durumu
public $sor; // mysql_query ile birleştirilen hali
public $kacsonuc;
function db(){
$this->durum = mysqli_connect($this->server,$this->suser,$this->spass);
$this->dbchoose($this->db)
}
function dbchoose($db){
if(!mysqli_select_db($this->durum,$db)){
$this->errors[] = 'Database cant choose';
$this->hata=True;
}
function see(){
$id = 'select * from uyeler where id=?';
$sor = mysqli_prepare($this->durum, $id);
$hede = '3';
mysqli_stmt_bind_param($sor, 's', $hede);
mysqli_stmt_execute($sor);
mysqli_stmt_bind_result($sor, $bas);
mysqli_stmt_fetch($sor);
}
}
what is the problem ? I cant understand.Thank you (and sorry for my grammer).
Anytime you see "boolean given" in a mysql error message, it means that some previous mysql function call has failed, returning a boolean FALSE value. You're trying to use that boolean false in the current mysql call, and get this message.
You have no error handling in your code, which means those FALSE values will propagate throughout your code, spitting out those errors everywhere it goes.
At minimum, your code should look something like this, everywhere you do a mysql/mysqli call:
function see(){
$id = 'select * from uyeler where id=?';
$sor = mysqli_prepare($this->durum, $id);
if ($sor === FALSE) {
die(mysqli_error($this->durm));
}
etc...
}
How you handle the error is up to you - in this case it'll simply abort the script and tell you why.

Categories