php - FullCalendar on Wordpress 集成 - wpdb 和 json 问题

标签 php mysql wordpress fullcalendar

我正在寻求在 Wordpress 中使用 fullcalendar 的帮助。

我正在集成 phpmyadmin 中名为 wp_evenement 的自定义表的计划

所以,我有两个文件

一个带有模板页面,其中显示日历并带有来自 fullcalendar 的脚本。 这调用了另一个名为“events.php”的文件,我试图从数据库中获取 json 结果。

我尝试使用 $wpdb 但它不起作用(或者我无法使其工作)。 我尝试使用新的 PDO 连接到数据库,但它仅在我以管理员身份连接时才有效...

有人帮助我解决全局 $wpdb 解决方案吗?

这是我的代码:

模板:

<div id='calendar' style="margin-top:100px;"></div>    
<script>
            var $ = jQuery;
            $(document).ready(function() {
              var date = new Date();
              var d = date.getDate();
              var m = date.getMonth();
              var y = date.getFullYear();


              var calendar = $('#calendar').fullCalendar({
               editable: true,
               header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
               },


               events: "https://www.exemple.org/wp-content/mytheme/fullcalendar/events.php/",


               eventRender: function(event, element, view) {
                if (event.allDay === 'true') {
                 event.allDay = true;
                } else {
                 event.allDay = true;
                }
               }

              });

             });

            </script>

events.php

  <?php


// List of events
 $json = array();

 // Query that retrieves events
 $requete = "SELECT * FROM wp_evenement ORDER BY id";

 // connection to the database
 try {
 $bdd = new PDO('mysql:host=***.mysql.db;dbname=****', '***', '***');
 } catch(Exception $e) {
  exit('Unable to connect to database.');
 }
 // Execute the query
 $resultat = $bdd->query($requete) or die(print_r($bdd->errorInfo()));

 // sending the encoded result to success page
 echo json_encode($resultat->fetchAll(PDO::FETCH_ASSOC));

?>

尝试使用 $wpdb 有效!

    <?php

    include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php';
    include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
    include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php';


    // List of events
    $json = array();

    // Query that retrieves events
    global $wpdb;
    $requete = "SELECT * FROM wp_evenement ORDER BY id";

    // Execute the query
    $resultat = $wpdb->get_results($requete);

    // sending the encoded result to success page
    echo json_encode($resultat);

?>

最佳答案

试试这个代码。

<?php

    include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php';
    include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
    include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php';


    // List of events
    $json = array();

    // Query that retrieves events
    global $wpdb;
    $requete = "SELECT * FROM wp_evenement ORDER BY id";

    // Execute the query
    $resultat = $wpdb->get_results($requete);

    // sending the encoded result to success page
    echo json_encode($resultat);

?>

您的代码有拼写错误

尝试用 $wpdb 窃取 $wpbd

关于php - FullCalendar on Wordpress 集成 - wpdb 和 json 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48908342/

相关文章:

mysql - 如何通过linux shell脚本执行sql命令?

java - 如果图片尺寸过大,将无法加载

mysql - 自定义菜单保存在 Wordpress 3.0 数据库中的什么位置?

php - mysql View 减少

PHP 数组作为单独的行插入到 MySQL 表中。

php - 如何在 PHP Laravel Lumen 中建立与数据库的连接?

javascript - jQuery Isotope 不应用布局。继承问题?

jquery - Wordpress ajax 分页

java - 小型 JSP 应用程序不起作用

php - 如何防止 PHP 中的 SQL 注入(inject)?