Query is as following
SELECT `sID` FROM `subscribers` where `Email` LIKE '%xxx@xxx.com%'
and
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.
The following are the problems I found:
wp-config.php
. Remove the piece of code that you use to connect and use the$wpdb
object to run your queries.$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 hereThis is how I would redo your code:
However, if you really want to use prepared statements, use this: