MySQLi_Query() Expects Parameter 1 to Be MySQLi, Object Given

Warning: mysqli_query() expects parameter 1 to be mysqli, object given in [class file]

I wouldnt try to return anything inside the constructor. Why not call a separate fuction to get the connection?

 class Database {

function __construct() {}
function getConnection(){
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
return $connection;
}
}

and then get the connection from the Database object after instantiation:

$result = mysqli_query($db->getConnection(),"SELECT * from members WHERE email = '$emailusername' or username='$emailusername' and password = '$password'") or die(mysqli_error($db));

PHP mysqli_query() expects parameter 1 to be mysqli, object given in

mysqli_query expects a mysqli connection, but you give it a Db_conn object. This is what the error message says.

You must first connect and then give this (mysqli) connection to mysqli_query, e.g.

public function update($query){
$dbconn = new Db_conn();
$conn = $dbconn->connect();
if (mysqli_query($conn, $query)) {
return True;
} else {
return False;
}
}

PHP Warning: mysqli_query() expects parameter 1 to be mysqli, null given

Since you are using Wordpress, I advise you to use their $wpdb global object which you can use by first calling it globally: global $wpdb;. Then you will have functions to use like $wpdb->get_results( 'query', output_type ); instead of the mysqli functions. See the WP codex

But if you would like to use mysqli_ functions you still can use $wpdb which have a mysqli object which you can access by: $wpdb->dbh. That will be your mysqli connection object that you need for mysqli_ functions.

To apply this to your code:

// Add Signature Image after single post and page
add_filter('the_content','add_signature', 1);
function add_signature($text) {
global $post, $wpdb;
if(($post->post_type == 'post') || ($post->post_type == 'page')){
//$sql_site_d = "select * from orders_discounts";
$sql_site_d = "select * from orders_discounts where url = 'homeworkmaid.com' and status =1";

$rs_results_site_d = mysqli_query($wpdb->dbh, $sql_site_d) or die(mysqli_error($wpdb->dbh));
$total_site_d = mysqli_num_rows($rs_results_site_d);
if ($total_site_d > 0){
$row_site_d = mysqli_fetch_array($rs_results_site_d);
....
}
}
}

Modified lines:

Line 4: global $post, $wpdb;

Line 9: $rs_results_site_d = mysqli_query($wpdb->dbh, $sql_site_d) or die(mysqli_error($wpdb->dbh));

mysqli_query() expects parameter 1 to be mysqli, object given in /Applications/MAMP/htdocs/

The code is mixing PDO and mysqli interfaces. And that won't work.

$conn = new PDO("...");

mysqli_query($conn, ... );

If we create a PDO connection, then we use PDO interface functions.

If we use mysqli_ functions, then we create a mysqli connection.

mysqli_query() expects parameter 1 to be mysqli, object given in

Your

$db = new DB_CONNECT();

returns an instance of DB_CONNECT class.

What you need in your

mysqli_query(...)

call is the object returned by your mysqli_connect call.
Try changing your:

function __construct() {
// connecting to database
$this->connect();
}

with:

function __construct() {
// connecting to database
$this->conn = $this->connect();
}

Then, change:

$result =mysqli_query($db,"INSERT INTO person (nom,pass_world) VALUES ('$name', '$pass')");

with:

$result =mysqli_query($db->conn,"INSERT INTO person (nom,pass_world) VALUES ('$name', '$pass')");

Also, I think you should read the PHP manual for the functions you are using:
mysqli_connect
mysqli_select_db

Warning: mysqli_query() expects parameter 1 to be mysqli, object given

I think you problem is for connection
Try this code

$conn = mysqli_connect($DB_HOST,$DB_USER,$DB_PWD,$DB_NAME);

and also change

$query = "INSERT INTO ".$RECORD_TABLE."(cus_name,description,amount) VALUES ('".$cus_name."','".$description."','".$amount."') " ;
$result = $conn->query($query);

mysqli_query() expects parameter 1 to be mysqli boolean given while inserting unicode data to database

In your query you passing argument which Return Value: TRUE on success. FALSE on failure.

mysqli_query({database connection object}, {your desired query})

    function insert(){

$con = mysqli_connect('localhost','root','','db_darta');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
mysqli_set_charset($con,"utf8"); // returns true or false
mysqli_query($con,"insert into tbl_darta (fiscal_year, darta_no, darta_date, patra_date, paper_from, letter_recievedby, subject, file_upload, remarks, created_by, created_date, section) values ('$this->fiscal_year', '$this->darta_no', '$this->darta_date', '$this->patra_date', '$this->paper_from', '$this->letter_recievedby', '$this->subject', '$this->file_upload', '$this->remarks','$this->created_by','$this->created_date','$this->section')");
}

How to fix: Warning: mysqli_query() expects parameter 1 to be mysqli?

$link = mysqli_connect($DB_HOST, $user, $pass, $dbName);

mysqli_query($link, 'CREATE TEMPORARY TABLE `table`');

With mysqli you need to save the connection in variable and provide that for every time you use mysqli.

EDIT: generally: You need to provide the connection for every mysqli-related function you use.



Related Topics



Leave a reply



Submit