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.
mysqli_query() expects parameter 1 to be mysqli, resource given in ~\index.php on line 46
You're mixing MySQL API's (mysql_*
and mysqli_*
)
You have to use something like this:
$con = mysqli_connect("localhost","root","") or die ("could not connect");
And to select your database (mysqli_select_db
):
mysqli_select_db("s3f") or die ("no database");
Also for your connection error handling use (mysqli_connect_error()
):
mysqli_connect_error()
Side note:
If you're in a testing environment i would recommend you to put error reporting at the top of your file(s):
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
?>
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;
}
}
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.
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));
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in?
To output the data you need to use one of the fetching methods - in this case mysqli_fetch_assoc
<?php
/* be consistent with the name of the db connection object - $link */
$link=mysqli_connect("localhost","xxxx","xxxxxxxxx","jbts");
if( !$link ) exit( "MySQL error : ". mysqli_connect_error() );
mysqli_set_charset( $link, "utf8" );
$sql = "select * from `Plot-log`";
$result = mysqli_query( $link, $sql );
?>
The names below need to be changed to the actual names of the columns in the table.
<?php
if( $result ){
while( $rs=mysqli_fetch_assoc( $result ) ){
echo "
<tr>
<td>{$rs['FIELD_NAME_1']}</td>
<td>{$rs['FIELD_NAME_2']}</td>
<td>{$rs['FIELD_NAME_3']}</td>
</tr>";
}
}
?>
Related Topics
Connecting to Remote MySQL Server Using PHP
Remove Duplicate from String in PHP
Php: Check If a File Is Loaded Directly Instead of Including
When to Use HTMLspecialchars() Function
PHP - Include a PHP File and Also Send Query Parameters
PHP Creating Zips Without Path to Files Inside the Zip
Shorthand for Arrays: Is There a Literal Syntax Like {} or []
Fetching Single Row, Single Column with Pdo
.Htaccess Rewriterule to Preserve Get Url Parameters
Algorithm That Will Take Numbers or Words and Find All Possible Combinations
Convert Utc Offset to Timezone or Date