admin管理员组

文章数量:1415111

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.

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.

Share Improve this question edited Aug 26, 2019 at 6:16 Bertrand asked Aug 22, 2019 at 5:22 BertrandBertrand 11 bronze badge 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 Commented Aug 22, 2019 at 7:50
Add a comment  | 

1 Answer 1

Reset to default 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;

?>

本文标签: databaseTroubles when I want to connect to another DB