duanbeng1923 2013-01-30 22:15
浏览 37
已采纳

从laravel控制器访问wpdb(wordpress数据库)

The application I am working on consists of Laravel and Wordpress. I have all the data required in the wordpress database (as additional plugins if need be). I need to be able to connect to the wordpress' wpdb from the laravel controller in order to return the correct view.

however, when I include the wordpress' load.php

(require_once(<wp root>/wp-load.php)

I get the following error when accessing the laravel page:

Cannot redeclare __() (previously declared in  /www/laravel/helpers.php:24)

this is because I am trying to use $wpdb to access the DB to get the cotents.

Any ideas on a workaround?

  • 写回答

1条回答 默认 最新

  • douwen1006 2013-01-31 00:58
    关注

    The problem is that wp-load.php boots most of the WordPress framework and WordPress has a function called __(). Apparently, so does Laravel. I tried booting wpdb by itself like this:

    include('/path/to/wordpress/wp-includes/wp-db.php');
    $mydb = New wpdb('user', 'pass', 'dummydb', 'localhost');
    $test = $mydb->get_results("SELECT * FROM {$mydb->posts} LIMIT 5");
    

    But it will throw undefined function errors because it uses functions in the rest of the WordPress code base, which isn't being loaded. That means you aren't going to be able to use $wpdb and Laravel without that function name conflict.

    You really don't need $wpdb though, at least I don't know why you would. It isn't really much more than a fairly limited wrapper around PHP's mysql_* functions. It is a (minor) convenience but that is all. If you have database connection information you can do about the same thing with straight PHP.

    If you needed to use WP_Query I'd understand. Some of what it does would be very painful to write by hand.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频