使用wordpress wp-config文件连接数据库
-
-
请解释_为什么准确_不能作为插件.Please explain _why exactly_ it's not possible as plugin.
- 1
- 2012-05-04
- kaiser
-
由于该脚本需要公开访问,而不是在管理员端(由于登录屏幕可能会碰到,因此无法在wp-content/plugins之类的任何文件夹上运行).Because the script require to be publicly accessed, not on the admin side( it will not work on any folder like wp-content/plugins since a login screen may come across ).
- 0
- 2012-05-04
- user983248
-
我认为您可能想编辑问题,以说明要对脚本进行的操作.几乎所有东西都可以作为插件:)I think you might want to edit your question to say what you want to do with your script. Pretty much anything is possible as a plug-in :)
- 0
- 2012-05-04
- Stephen Harris
-
Paypal的IPN验证,请参阅,它在Plugins文件夹中对我不起作用,但在整个Wordpress安装之外的文件夹中对我来说均无效IPN validation for Paypal, See, it didn't work for me while doing it from the Plugins folder, but yes from a folder outside the whole Wordpress installation
- 0
- 2012-05-04
- user983248
-
2 个回答
- 投票数
-
- 2012-05-04
使用用户在wp-config中设置的定义:
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
编辑: 由于您的脚本不在Wordpress环境中,因此您需要在使用wp-config中的定义之前启动它.
require_once('./path/to/the/wp-config.php'); mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
Using the defines the user sets in wp-config:
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
EDIT: Since your script is outside the Wordpress environment, what you want to do is initiate it before using the defines in wp-config.
require_once('./path/to/the/wp-config.php'); mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
-
您曾说过,目前它不适用于您自己的问题.不会拒绝投票,但请确保您的答案确实有效,并显示操作程序的错误之处.谢谢!:)顺便说一句:欢迎使用WPSE,不要让我这么小的推动使您无法回答其他问题.回答总是受到高度赞赏.Which you stated that it currently doesn't work in your own question. Not going to downvote, but please make shure that your answer really works and shows what the OP makes wrong. Thanks! :) Btw: Welcome to WPSE and don't let that little push by me hold you back from answering other questions. Answering is always highly appreciated.
- 1
- 2012-05-04
- kaiser
-
与数据库的连接正常.我的问题是将调用它的函数抽象到外部文件.我在此处使用的值是在" wp-config.php"中定义的设置,可用于设置Wordpress.您是否至少在尝试不起作用之前尝试过它?The connection to the database works. The problem in my question is abstracting the function that calls it out to an external file. The values I'm using here are defines set in `wp-config.php` which you use to set up Wordpress. Did you at least try it before assuming it doesn't work?
- 0
- 2012-05-04
- akamaozu
-
这与最初的问题有点不同This is a bit off the original question
- 0
- 2012-05-04
- user983248
-
您实际尝试过吗?我遇到了与_(连接到数据库)_完全相同的问题,我通过使用wp-config _(如您要求的)_的定义集解决了它.您唯一需要的变量是$ db_name,因为所有其他变量都已经在WP环境中,这要归功于wp-config.php.只要加载了Wordpress环境,您就可以完全访问这些定义. **编辑:您的脚本是否在Wordpress环境之外?**Have you actually tried it? I had the exact same problem you have _(connecting to the database)_ and I solved it by using the defines set by wp-config _(like you requested)_. The only variable you need is $db_name, since all the rest are already in the WP environment thanks to `wp-config.php`. As long as the Wordpress environment is loaded, you have total access to the defines. **edit: Is your script outside the Wordpress environment?**
- 0
- 2012-05-04
- akamaozu
-
是的,请阅读我的最新修改,并感谢您抽出宝贵的时间Yes, please read my last edit, and thanks for taking the time
- 0
- 2012-05-04
- user983248
-
没问题测试了我的修复程序并编辑了原始解决方案以反映出来.Not a problem. Tested my fix and edited the original solution to reflect it.
- 0
- 2012-05-04
- akamaozu
-
@Akamaozu:将代码从" wp-blog-header.php"编辑为" wp-config.php"后,我将接受您的答案作为正确答案,因为这是此处涉及的文件.非常感谢@Akamaozu: I will accept your answer as the correct one after you edit the code from 'wp-blog-header.php' to 'wp-config.php' since that is the file in question here. Thanks a lot
- 0
- 2012-05-04
- user983248
-
- 2014-04-05
您可以使脚本成为WordPress帖子的一部分,只需使用WordPress本身提供的
$wpdb
对象.$wpdb
对象已经建立了数据库连接,您可以使用它执行任何数据库操作:插入,更新,查询等.这是在您执行WordPress内DB事务时的首选方法不必打开任何其他数据库连接.这是获取未来职位的简单示例,例如:
$posts = $wpdb->get_results("SELECT ID, post_title FROM wp_posts WHERE post_status = 'future' AND post_type='post' ORDER BY post_date ASC LIMIT 0,4");
请查看本文以获取更多信息: http://wp.smashingmagazine.com/2011/09/21/interacting-with-the-wordpress-database/
You can make your script a part of your WordPress post, just use the
$wpdb
object provided by the WordPress itself. The$wpdb
object already has the database connection established and you can use it to perform any database operation: insert, update, query etc... This is preferable method for doing you DB stuff inside WordPress as you do not have to open any additional database connections.Here is a simple example for getting the future posts for instance:
$posts = $wpdb->get_results("SELECT ID, post_title FROM wp_posts WHERE post_status = 'future' AND post_type='post' ORDER BY post_date ASC LIMIT 0,4");
Check out this article for additional info: http://wp.smashingmagazine.com/2011/09/21/interacting-with-the-wordpress-database/
-
当我从您的答案中删除链接时,除了暗示$ wpdb可以执行基本的数据库任务之外,我没有任何关于实际解决方案的信息.您能否介意改善答案以展示一些基本示例?谢谢.When I remove the link from your answer, I got no information about what the actual solution would be, aside from a hint that `$wpdb` can perform basic database tasks. Would you please mind to improve your answer to show off some basic example? Thanks.
- 1
- 2014-04-05
- kaiser
-
那里的文章对$ wpdb对象有非常详细的描述,所以我不想在那里剪切和粘贴很多文本.但是基本上,如果您的脚本是WordPress的一部分,则可以使用$ wpdb对象运行数据库查询,如下所示: $posts=$ wpdb->get_results("选择ID,来自wp_posts的post_title,其中post_status='future'和post_type='post'ORDER BYpost_date ASC LIMIT 0,4"); 提出问题的人后来澄清说,他不想将其做成插件,所以我的回答现在不那么重要了,所以我决定将其保留.The article there has a very detailed description of the `$wpdb` object, so I didn't want to the cut and paste a lot of text there. But basically if your script is part of the WordPress, you can use the `$wpdb` object to run the database queries like this: `$posts = $wpdb->get_results("SELECT ID, post_title FROM wp_posts WHERE post_status = 'future' AND post_type='post' ORDER BY post_date ASC LIMIT 0,4");` The person asking the question clarified it later that (s)he does not want to make it a plugin, so my answer is less relevant now, so I decided to leave it as is.
- 0
- 2014-12-11
- obaranovsky
-
请始终将任何需要的信息放入问题中.评论会定期清理.无论如何,我又读了另一个答案和问题,并将两者都-1.到现在为止,原始问题仍然看起来像是企图入侵/感染网站,而另一个答案与每一行中的最佳实践都背道而驰.Please always put any information one needs into the question. Comments get cleaned up regularly. Anyway, I read the other answer and the question again and -1ed both of them. By now the original question still looks like an attempt to hack/infect a site and the other answer is against best practice in every single line.
- 0
- 2014-12-11
- kaiser
-
我认为这是更好的解决方案.最好使用内置的WordPress功能.查看$ wpdb对象后,它应该变得清晰.This is the better solution in my opinion. Making use of build-in WordPress functions is always preferable. After looking in $wpdb Object it should become clear.
- 0
- 2017-06-14
- user3135691
如何使用wp-config.php文件连接到数据库?
我正在尝试使脚本对Wordpress更友好,并且我需要连接到数据库,但无需将脚本安装为插件.
基本上我的剧本上有
该脚本不能作为插件安装(这可以使事情变得更容易),因此我需要使用安装时使用的现有wp-config.php连接到数据库. >
预先感谢
修改和澄清
1-我需要按原样使用wp-config.php,无需修改. 2-脚本位于www.example.com/script/ 3-不能作为插件完成,因为脚本的核心要求公开访问,而没有任何登录屏幕跳来跳去. 4-我的问题基本上是如何通过修改上面的脚本使用wp-config.php文件连接到数据库.