Cannot get results from WordPress database using LIKE

Query is as following

SELECT `sID` FROM `subscribers` where `Email` LIKE '%xxx@xxx.com%'

and

Read More
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
$hostname = "dd";
$username = "cc";
$password = "aa";
$dbName = "bb";


$conn = new mysqli($hostname, $username, $password);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$conn -> select_db("$dbName") or die( "Unable to select database");

check_subscriber($email, $first, $last, $company, $tablename);
function check_subscriber($email, $first, $last, $company, $tablename)
{
    global $wpdb; 
    $emailid=mysql_real_escape_string($email);
    $subscribercheck = "SELECT `sID` FROM `$tablename` where `Email` LIKE '%$emailid%'";
    $subscriber_exists = "";
    $subscriber_exists = $wpdb -> get_results($wpdb->prepare("$subscribercheck",object));
    var_dump($subscriber_exists);

    if(count($subscriber_exists)>0){
        foreach ($subscriber_exists as $subs)
        {
            echo $sid = $subs-> sID;
        }
    }
    else {
        $wpdb -> insert('subscribers', array(
            'First Name' => $first,
            'Last Name' => $last,
            'Company' => $company,
            'Email' => $email,
            'Date' => date("Y-m-d H:i:s"),
        ));
    }
}

var_dump = NULL

I tried running the query in our database and it worked, but it doesn’t seem to be running here. Please help!

Every variables are defined. var_dump = NULL so it goes to "else" clause where wpdb -> Insert happens and works.

$subscriber_exists = $wpdb -> get_results($wpdb->prepare("$subscribercheck",object));

However, does not give me any value even though I can run it in our database.

Related posts

1 comment

  1. The following are the problems I found:

    • you are attempting a database connection manually while WordPress already sets this up for you via wp-config.php. Remove the piece of code that you use to connect and use the $wpdb object to run your queries.
    • you are using $wpdb->prepare() but you are not setting the necessary parameters correctly. If you want to use prepared statements you need to understand how they work first, read here
    • since you are not making use of prepared statements then run your normal query without it.
    • Make sure that your table is located in the same database that wordpress is setup for.

    This is how I would redo your code:

    function check_subscriber($email, $first, $last, $company, $tablename) {
        global $wpdb;
    
        $email = $wpdb->esc_like($email);
    
        $query = "SELECT `sID` FROM `$tablename` where `Email` LIKE '%$email%'";
    
        $subscribers = $wpdb->get_results($query, object);
    
        if (count($subscribers) > 0) {
            foreach ($subscribers as $subs) {
                echo $sid = $subs->sID;
            }
        } else {
            $wpdb->insert('subscribers', array(
                'First Name' => $first,
                'Last Name' => $last,
                'Company' => $company,
                'Email' => $email,
                'Date' => date("Y-m-d H:i:s"),
            ));
        }
    }
    

    However, if you really want to use prepared statements, use this:

    function check_subscriber($email, $first, $last, $company, $tablename) {
        global $wpdb;
    
        $query = "SELECT `sID` FROM `$tablename` where `Email` LIKE %s";
    
        $like_email = '%' . $wpdb->esc_like($email) . '%';
    
        $subscribers = $wpdb->get_results(
            $wpdb->prepare($query, $like_email),
            object
        );
    
        if (count($subscribers) > 0) {
            foreach ($subscribers as $subs) {
                echo $sid = $subs->sID;
            }
        } else {
            $wpdb->insert('subscribers', array(
                'First Name' => $first,
                'Last Name' => $last,
                'Company' => $company,
                'Email' => $email,
                'Date' => date("Y-m-d H:i:s"),
            ));
        }
    }
    

Comments are closed.