Asked  7 Months ago    Answers:  5   Viewed   87 times

Sorry for the duplicate question but can someone please help me solve this? Am a PHP newbie and none of the solutions seem to be working.

Error Screeshot

Here's the code:

// Add Signature Image after single post and page
add_filter('the_content','add_signature', 1);
function add_signature($text) {
global $post;
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($sql_site_d) or die(mysqli_error());
$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);   

 Answers

86

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));

Saturday, May 29, 2021
 
Gilko
answered 7 Months ago
19

You need to give the array_shift() the parameter! Look this example:

$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack); // Here you give the parameter
print_r($fruit);

You give the null parameter on array_shift() and you need to change it!

Update:

array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched. Read here for more

Wednesday, March 31, 2021
 
hnkk
answered 9 Months ago
99

The error is fairly vague, but specific. It's stating that a connection object, mysql_result, is expected but it's receiving null instead.

The reason for this is how you're attempting to use the connection inside each function:

global $con;
mysqli_query($con, $q);

The global keyword will bring in global variables to the function you're using it in, but $con isn't a global variable in this class (and, you don't need to use global inside classes). What you're looking for instead is the $this keyword.

In your class constructor MySQLDB() you define the $con variable; you should instead change this to use $this->connection as you've already defined a global variable named $connection:

$this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS,DB_NAME);

Then, in each function, instead of using something like:

global $con;
mysqli_query($con,$q);

You can use:

mysqli_query($this->connection, $q);
Saturday, May 29, 2021
 
EastSw
answered 7 Months ago
39

yo need create the user "pma" in mysql or change this lines(user and password for mysql):

/* User for advanced features */
$cfg['Servers'][$i]['controluser'] = 'pma'; 
$cfg['Servers'][$i]['controlpass'] = '';

Linux: /etc/phpmyadmin/config.inc.php

Tuesday, July 13, 2021
 
ShadowZzz
answered 5 Months ago
36

This is how your code should look (with added SQL Injection protection):

<?php
include "dbinfo.php"; //contains mysqli_connect information (the $mysqli variable)
//inputs
$name = mysqli_real_escape_string($_GET['name']);
$text = mysqli_real_escape_string($_GET['text']);

$sqlqr = "INSERT INTO `ncool`.`coolbits_table` (`name`, `text`, `date`) VALUES ('" . $name . "', '" . $text . "', CURRENT_TIMESTAMP);";

mysqli_query($mysqli,$sqlqr); //function where the magic happens.
?>

Take a look at what I've done. Firstly I've escaped the user input you're retrieving into the $name and $text variables (this is pretty much a must for security reasons) and as others have suggested you should preferably be using prepared statements.

The problem is that you weren't surrounding string values with single quotes ('), which is a requirement of the SQL syntax.

I hope this helps to answer your question.

Thursday, September 2, 2021
 
Pachvarsh
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share