0

I want to connect my Wordpress website 1 to database from Wordpress website 2.

Here is my code:

<?php

$myotherdatabase = new wpdb("username","password","databasename","localhost");

$motscles = "abri";

$query = "SELECT `post_id` FROM `wp_postmeta` WHERE `meta_key` = 'nom' AND MATCH (`meta_value`) AGAINST ('" . $motscles . "' IN BOOLEAN MODE)";

$result = $myotherdatabase -> get_results($query, ARRAY_A);

foreach ($result as $post) {
  $pids .= $post['post_id'] . ",";// ARRAY_A
}

echo $pids;

?>

The result is empty. If I run the same query directly on Wordpress website 2, it works.

Therre is something wrong in my code? Or do you think the problem may come from hosting?

Thank you


EDIT: The problem came from the hosting. There is a restriction, I can not connect to the website 2 database from website 1.

  • Do you run the same script, or just the SELECT query in phpMyAdmin e.g. on your website 2? Is the 'wp_' prefix correct in website 2? – moped Aug 22 at 7:50
0

Use code as below :

<?php

$myotherdatabase = new wpdb("username","password","databasename","localhost");

$motscles = "abri";

$query = "SELECT `post_id` FROM `wp_postmeta` WHERE `meta_key` = 'nom' AND MATCH (`meta_value`) AGAINST ('" . $motscles . "' IN BOOLEAN MODE)";

$result = $myotherdatabase -> get_results($query, ARRAY_A);

foreach ($result as $post) {
  $pids .= $post->post_id . ",";// ARRAY_A
}

echo $pids;

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.