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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分