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 - 可以使用表中的主键 INT 来引用 MySQL 中的另一个表名吗?

jquery - 如何从另一张图像打开特定图像的颜色框?

php - 如何检查你的代码环境是在 Windows 上还是在 Linux 或其他操作系统上

mysql - MySQL是否维护您正在查询的表的顺序

PHP Node JS 身份验证

php - 如何确保处理 ajax 请求的 PHP 脚本不可访问且用户已通过身份验证?

php - 插入复制第一个插入,但不复制任何其他插入

mysql - 根据自定义计算字段过滤行

php - wp-bootstrap-navwalker 断点更改

php - enctype ="multipart/form-data"和 enctype ="multipart/form-data;charset=utf-8"有什么区别