Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
As mentioned in comments, this is a scoping issue. Specifically, $con
is not in scope within your getPosts
function.
You should pass your connection object in as a dependency, eg
function getPosts(mysqli $con) {
// etc
I would also highly recommend halting execution if your connection fails or if errors occur. Something like this should suffice
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // throw exceptions
$con=mysqli_connect("localhost","xxxx","xxxx","xxxxx");
getPosts($con);
mysqli_query() expects parameter 1 to be mysqli, null given in mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in
You have a typo. It should be __construct()
notice the extra _
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>";
}
}
?>
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.
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 on line 26
If you have defined, $link=mysqli_connect()
outside the function, you need to pass it as a parameter to the function or declare it global
inside the function.
1. Passing as parameter
yourFunction($link); //function call, passing the variable to the function
function yourFunction($link) //you can use any name for parameter
{ //now it can be accessed here
}
2. Declaring as Global inside the function
function yourFunction()
{
global $link;
//now it can be accessed here
}
By using the keyword global
, you are specifying that you want to use the $link
which you have defined in global scope
.
You may have to do the same with
$MySQLi
and other variables if you
have defined them outside the function.
Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /users/mikadoru/www/register.php on line 16
You are not opening any DB!
From PHP manual:
mysqli_connect($db_host,$db_user,$db_pass,$db_database,$db_port);
Related Topics
Replacing Accented Characters PHP
PHP Exec Command (Or Similar) to Not Wait For Result
How to Have a 64-Bit Integer in PHP
List of All Locales and Their Short Codes
PHP: Is MySQL_Real_Escape_String Sufficient For Cleaning User Input
Json_Decode Returns Null After Webservice Call
I Have an Array of Integers, How to Use Each One in a MySQL Query (In PHP)
How to Filter a Two Dimensional Array by Value
How to Enable Curl in PHP/Xampp
Simple Jquery, PHP and Jsonp Example
Is There Way to Use Two PHP Versions in Xampp
Get Sum of MySQL Column in PHP