I am trying to add data from another website to my Wordpress site. For that I have created a Plugin. In this Plugin I can get data from my other website, but i can't and it to the my Wordpress Website. Below is my code for that.
$mysqlis=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$slug = str_replace(" ","-",$value);
$checkdublicate = $mysqlis->query("SELECT * FROM `temp_wp_terms` WHERE `slug` = '.$slug.' AND `name`='.$value.'");
if ($checkdublicate->num_rows == "0")
{
if ($value != "." && $value != "..")
{
$query = "INSERT INTO `temp_wp_terms`(`name`, `slug`, `term_group`) VALUES ('.$value.', '.$slug.', '0')";
$mysqlis->query($query);
if(!$mysqlis)
{
echo "Error in insert";
}
}
else
{
mylog("173 insert failed NoRow: " .print_r($checkdublicate->num_rows,TRUE));
}
}
$mysqlis->close();
Please give me any solution. Thank you
If you're using a plugin you should use the global $wpdb object rather than mysqli. $wpdb provides a way to prepare custom queries and automatically uses the database credentials in the wordpress config file.
try something like this, (the replace query will avoid duplicates if you have proper indexes set.):
global $wpdb;
$query = $wpdb->prepare( "REPLACE INTO `temp_wp_terms`(`name`, `slug`, `term_group`) VALUES (%s, %s, %d)", $value, $slug, 0 );
$wpdb->query( $query )
Source: https://developer.wordpress.org/reference/classes/wpdb/