使用wpdb连接到单独的数据库
-
-
另一个MySQL数据库,还是另一个数据库类型?您是否仍想访问常规的WordPress数据库,还是要将网站从一个数据库移到另一个数据库?Another MySQL database, or another database type? Do you still want access to the regular WordPress database, or are you moving the site from one DB to another?
- 0
- 2010-09-10
- EAMann
-
是的,另一个MySQL数据库.它是同一台服务器上的一个单独的数据库,不是Wordpress的.这是一个自定义数据库,其中包含我想在wordpress中显示的信息.Yes, another MySQL database. It's a separate DB on the same server, and it's not a Wordpress one. It's a custom db, with information I want to display inside wordpress.
- 1
- 2010-09-10
- Wadih M.
-
如果使用$ wpdb对象执行此操作,甚至有可能,它将使WordPress其余部分与现有数据库断开连接.所以,不推荐.另一个选择是使用WordPress使用的EZSQL创建新实例.我认为使用EZSQL是因为它是使您不必使用php-pdo-mysql,php-mysql或php-mysqli的抽象层,而不知道可能会在给定服务器上安装哪个层.If you did that with the $wpdb object, if it were even possible, it would disconnect the rest of WordPress from its existing database. So, not recommended. Another option is to create a new instance using EZSQL, which is used by WordPress. I think EZSQL is used because it's a layer that abstracts you from having to use php-pdo-mysql, php-mysql, or php-mysqli, not knowing which might be installed on a given server.
- 1
- 2010-09-10
- Volomike
-
是的,有可能.可以实例化wpdb以访问任何数据库并查询任何表.Yes it's possible. wpdb can be instantiated to access any database and query any table.
- 3
- 2010-09-10
- Wadih M.
-
6 个回答
- 投票数
-
- 2010-09-10
是可能的.
wpdb对象可用于访问任何数据库和查询任何表.完全不需要与Wordpress相关,这非常有趣.
好处是可以使用所有wpdb类和函数,例如
get_results
等,因此无需重新发明轮子.方法如下:
$mydb = new wpdb('username','password','database','localhost'); $rows = $mydb->get_results("select Name from my_table"); echo "<ul>"; foreach ($rows as $obj) : echo "<li>".$obj->Name."</li>"; endforeach; echo "</ul>";
Yes it's possible.
The wpdb object can be used to access any database and query any table. Absolutely no need to be Wordpress related, which is very interesting.
The benefit is the ability to use all the wpdb classes and functions like
get_results
, etc so that there's no need to re-invent the wheel.Here's how:
$mydb = new wpdb('username','password','database','localhost'); $rows = $mydb->get_results("select Name from my_table"); echo "<ul>"; foreach ($rows as $obj) : echo "<li>".$obj->Name."</li>"; endforeach; echo "</ul>";
-
布阿太糟糕了,所有这些评论加在问题本身上,以致您无法准确回答.Booyah. Too bad all those comments added up on the question itself to block your accurate answer.
- 4
- 2010-09-11
- jerclarke
-
@杰里米·克拉克:我同意.希望我们的压词者更加谨慎,不要无辜地散布虚假信息.@Jeremy Clarke: I agree. Hoping our fellow wordpressers will be more careful to not innocently spread out disinformation.
- 0
- 2010-09-20
- Wadih M.
-
您也可以使用`global $ wpdb`来节省时间.但是在触发$ wpdb->get_results方法之前,您必须包括wp-load.php作为: `require_once('/your/wordpress/wp-load.php');`you can also save time by using `global $wpdb`. But before firing $wpdb->get_results method, you must include wp-load.php as: `require_once('/your/wordpress/wp-load.php');`
- 1
- 2015-09-19
- Junior Mayhé
-
设置WPDB前缀以使WP_Query和get_post通过调用`$mydb-> set_prefix('wp_');生成正确的sql查询.Set WPDB prefix to make WP_Query and get_post to generate correct sql query by calling `$mydb->set_prefix('wp_');`
- 0
- 2015-10-13
- M-R
-
我知道这是一个旧线程,但是我忍不住用新对象压扁$mydb变量可能会使连接保持打开状态(我可能错了).我将检查是否已经从先前的调用中实例化了$mydb,如果是,在旋转新实例之前关闭连接.例如(抱歉,注释中不能包含整洁的Markdown代码块): `if($mydb!=null){$mydb-> close();}`I know this is an old thread, but I can't help but feel squashing the `$mydb` variable with a new object could leave a connection open (I could be wrong). I would check to see if `$mydb` is already instantiated from a previous call, and if so, close the connection before spinning up a new instance. eg (sorry can't do neat Markdown code blocks in the comments): `if ($mydb != null) { $mydb->close(); }`
- 1
- 2020-01-21
- joehanna
-
- 2010-09-11
在WordPress中,连接第二个数据库很容易,您只需创建WPDB类的新实例,并以与使用我们都知道并喜欢的标准$ wpdb实例相同的方式使用它.
假设第二个数据库具有与主WP相同的登录信息,您甚至可以使用wp-config.php中的预定义常量来避免对登录信息进行硬编码.
/** * Instantiate the wpdb class to connect to your second database, $database_name */ $second_db = new wpdb(DB_USER, DB_PASSWORD, $database_name, DB_HOST); /** * Use the new database object just like you would use $wpdb */ $results = $second_db->get_results($your_query);
Connecting to a second database is easy in WordPress, you simply create a new instance of the WPDB class and use it the same way you would use the standard $wpdb instance we all know and love.
Assuming the second database has the same login information as the main WP one you can even use the predefined constants from wp-config.php to avoid hardcoding the login information.
/** * Instantiate the wpdb class to connect to your second database, $database_name */ $second_db = new wpdb(DB_USER, DB_PASSWORD, $database_name, DB_HOST); /** * Use the new database object just like you would use $wpdb */ $results = $second_db->get_results($your_query);
-
这对于Wadih的回答来说有些多余,但是我认为我的代码示例更加清晰,并且记住db登录常数也很重要,因为它们几乎总是正确的使用方式,否则在从dev-> stage-迁移时会冒问题的风险.>实时环境,登录详细信息可能会更改.This is somewhat redundant to Wadih's answer but I think my code example is a bit clearer and its also important to remember the db login constant's as they are almost always the right ones to use and otherwise you risk issues when moving from dev->stage->live environments where the login details might change.
- 0
- 2010-09-11
- jerclarke
-
设置WPDB前缀以通过调用`$ second_db-> set_prefix('wp_');来使WP_Query和get_post生成正确的sql查询.Set WPDB prefix to make WP_Query and get_post to generate correct sql query by calling `$second_db->set_prefix('wp_');`
- 0
- 2015-10-13
- M-R
-
- 2010-12-29
没有人这么说,所以我想我会添加一种更简单的方法.
只要您的附加数据库具有与wordpress数据库相同的用户/访问权限详细信息即可访问,您可以在表名之前使用数据库名
$query = $wpdb->prepare('SELECT * FROM dbname.dbtable WHERE 1'); $result = $wpdb->get_results($query);
no one has said this so I thought I'd add an even easier way..
as long as your additional database has the same user/pass details to access it as your wordpress database you can use the database name before the table name like this
$query = $wpdb->prepare('SELECT * FROM dbname.dbtable WHERE 1'); $result = $wpdb->get_results($query);
-
根据我的经验,这仅适用于_get_数据,即使用`SELECT`.您不能插入数据.From my experience, this only works to _get_ data, i.e. using `SELECT`. You can't insert data.
- 0
- 2015-06-28
- Protector one
-
它在外部不起作用,it will not work externally,
- 0
- 2019-01-31
- Wasim A.
-
- 2011-04-08
虽然这些方法可以使用,但您将失去使用"其他"自定义功能(如get_post_custom和wordpress查询)的能力.简单的解决方法是
$wpdb->select('database_name');
更改系统范围的数据库(一个mysql select_db).如果只想进行简单查询,则使用database.table方法,但是如果要访问另一个wordpress博客,则可以使用select.完成后,您只需要将其更改回去,否则您的博客可能会做一些奇怪的事情.
While these will work, you'll lose the ability to use the "other" custom features such as get_post_custom and wordpress queries. The simple solution is
$wpdb->select('database_name');
which changes the database system-wide (a mysql select_db). The database.table method works if you just want to make a simple query, but if you want to access another wordpress blog you can use select. You'll just need to change it back when you're done or your blog may do strange things.
-
我正在使用此解决方案,并且效果很好,除了一件事.由于某些未知原因,`wp_get_post_terms()`似乎没有使用新选择的数据库?我尝试过的所有其他函数(例如get_post_meta(),get_posts()等)似乎都可以正常工作,但是wp_get_post_terms()似乎可以用于DB_NAME数据库.有任何想法吗?I'm using this solution and it works great, except for one thing. For some unknown reason `wp_get_post_terms()` doesn't seem to use the newly selected DB?? Every other function I've tried (like `get_post_meta()`, `get_posts()` etc) seems to work just fine but `wp_get_post_terms()` seems to work towards the `DB_NAME` database. Any ideas?
- 0
- 2013-07-09
- powerbuoy
-
- 2010-09-10
我无法发表评论,但我想扩大Wadih M.的回答(很棒).
WP的数据库类是Justin Vincent的ezSQL的自定义版本.如果您喜欢该界面,并且想要创建一个非基于WordPress的网站,则可能需要查看一下:http://justinvincent.com/ezsql
I can't comment yet, but I wanted to expand on Wadih M.'s answer (which is great).
WP's database class is a customized version of Justin Vincent's ezSQL. If you like the interface and you're wanting to do a site that's not WordPress-based, you might want to check it out: http://justinvincent.com/ezsql
-
ezSQL对我真的很沮丧,来自WPDB.没有"准备"语句,没有"插入"或"更新" ...我喜欢使用整个WPDB类,因为可以通过在项目中包含BackPress中的几个文件来实现.ezSQL was really frustrating for me, coming from WPDB. No "prepare" statements, no "insert" or "update"... I like to use the entire WPDB class as it exists, which is possible by including a couple files out of BackPress in your project.
- 0
- 2011-04-22
- goldenapples
-
@gabrielk链接已消失-新链接为:[1] [1]:http://justinvincent.com/ezsql@gabrielk The link is dead - new one is: [1] [1]: http://justinvincent.com/ezsql
- 0
- 2013-11-23
- Hexodus
-
- 2011-04-22
我正在努力使用
$wpdb
从需要更新两个博客的父站点连接到第二个博客数据库.我使用$wpdb->select($dbname, $dbh)
选择第二个数据库,但是我仍然从第一个数据库中获得结果.我通过在第二个数据库上调用WP函数之前调用
wp_cache_flush()
清除WordPress缓存来解决此问题.I was struggling with using
$wpdb
to connect to a second blog database from a parent site that needs to update two blogs. I used$wpdb->select($dbname, $dbh)
to select the second database, but I was still getting results from the first database.I resolved the problem by calling
wp_cache_flush()
to clear the WordPress cache before calling WP functions on the second database.
我想将
wpdb
连接到另一个数据库.如何创建实例并将其传递数据库名称/用户名/密码?谢谢