0

我在我的 Windows 服务器 2008 中有服务器 Mysql。PHP 工作,我测试我与我的数据库的连接,一切似乎都很好用 testmysql.php 示例:

<?PHP
//remember to change the password to whatever you set
//it to in mysql instance configuration

//first parameter is server name, 2nd username 'root', 3rd is password
$rst = @mysql_connect("localhost:3306","root","admin");

if (!$rst){
     echo( "<p>Unable to connect to database manager.</p>");
       die('Could not connect: ' . mysql_error());
     exit();
} else {
  echo("<p>Successfully Connected to MySQL Database Manager!</p>");
}

if (! @mysql_select_db("fullcalendar") ){
     echo( "<p>Unable to  connect database...</p>");
     exit();
} else {
  echo("<p>Successfully Connected to Database 'fullcalendar'!</p>");
}
?>

结果:成功连接到 MySQL 数据库管理器!

成功连接到数据库'fullcalendar'!

和我的文件 db.config.php

    <?php
/*
 * Created on 17-okt-2011
 * author Paul Wolbers
 */

function database_connect() {
    global $obj_db;

    if($_SERVER["HTTP_HOST"] == 'localhost:3306') {
        $str_hostname = 'localhost:3306';
        $str_username = 'root';
        $str_password = 'admin';
        $str_database = 'fullcalendar';
    } else {
        $str_hostname = 'localhost:3306';
        $str_username = 'root';
        $str_password = 'admin';
        $str_database = 'fullcalendar';
    }

    $obj_db = mysqli_connect($str_hostname, $str_username, $str_password, $str_database);
    if($obj_db === FALSE) {
        $error= "Database connection failed";
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    mysqli_set_charset( $obj_db , 'utf8' );
}
?>

我的 cal_events.php

<?php
/*
 * Created on 17-okt-2011
 * author Paul Wolbers
 *
 */
require_once '../include/default.inc.php';
//require_once '../config/db.config.php';
//database_connect();



// test with session user_id
$_SESSION['user_id'] = 1;

switch($_GET['action']) {
    case 'add':
        addEvent();
        break;
    case 'start':
        getEvents();
        break;
//  case 'get_agenda':
//      getEventsStartingToday();
//      break;
//  case 'update_title':
//      updateEventTitle();
//      break;
    case 'update':
        updateEvent();
        break;
    case 'resize':
        resizeEvent();
        break;
    case 'del':
        deleteEvent();
        break;
}

function getEvents() {
    global $obj_db;

    $str_query = 'SELECT *, event_id as id, concat_ws(" ",date_start,time_start) as start,concat_ws(" ",date_end,time_end) as end FROM events ';
    $obj_result = mysqli_query($obj_db, $str_query);

    while ($arr_line = mysqli_fetch_array($obj_result, MYSQLI_ASSOC)) {
        $arr_line['allDay']     = $arr_line['allDay'] == 0 ? false : true;
        $arr_line['editable']   = $arr_line['user_id'] == $_SESSION['user_id'] ? true : false;

        $arr_content[] = $arr_line;

    }

    echo json_encode($arr_content);
}

function getDaysBetween($sStartDate, $sEndDate){
    $sStartDate     = date("Y-m-d", strtotime($sStartDate));
    $sEndDate       = date("Y-m-d", strtotime($sEndDate));
    $aDays[]        = $sStartDate;
    $sCurrentDate   = $sStartDate;

    while($sCurrentDate < $sEndDate){
        $sCurrentDate = date("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));
        $aDays[] = $sCurrentDate;
    }
    return $aDays;
}

function getEventsStartingToday() {
    global $obj_db;

    $str_query = 'SELECT * , event_id AS id, concat_ws( " ", date_start, time_start ) AS
                    START , concat_ws( " ", date_end, time_end ) AS
                    END FROM events WHERE date_start >= DATE( NOW( ) )
                    OR (
                    date_start < DATE( NOW( ) )
                    AND date_end > DATE( NOW( ) )
                    )
                    ORDER BY date_start ';
    $obj_result = mysqli_query($obj_db, $str_query);

    while ($arr_line = mysqli_fetch_array($obj_result, MYSQLI_ASSOC)) {
        $arr_content[] = $arr_line;

    }
//print_r($arr_content);
    $arr_result = array();

    foreach($arr_content as $event) {
        // meerdaags event
        if($event['date_end'] != $event['date_start']) {

            $days_in_between = getDaysBetween($event['date_start'], $event['date_end']);
            foreach($days_in_between as $day) {
                $arr_result[$day][] = $event;
            }


        } else {
            $arr_result[$event['date_start']][] = $event;
        }
    }

    $obj_smarty = new Smarty();
    $obj_smarty->compile_dir = '../templates_c/';

    $obj_smarty->assign('items', $arr_result);

    $obj_smarty->display(FULLCAL_DIR.'/view/agenda.html');
}

function addEvent() {

    global $obj_db;
    if(!isset($_POST['date_end'])) {
        $_POST['date_end'] = $_POST['date_start'];
    }
    if(!isset($_POST['color'])) {
        $_POST['color'] = '';
    }
    $str_query = 'INSERT INTO events (title, user_id, color, date_start, time_start, date_end, time_end, allday) ' .
            'VALUES ("'.$_POST['title'].'",
                    '.$_SESSION['user_id'].',
                    "'.$_POST['color'].'",
                    "'.date('Y-m-d', $_POST['date_start']).'",
                    "'.date('H:i:s', $_POST['date_start']).'",
                    "'.date('Y-m-d', $_POST['date_end']).'",
                    "'.date('H:i:s', $_POST['date_end']).'"'.
                    (date('H:i:s', $_POST['date_start']) == '00:00:00' && date('H:i:s', $_POST['date_end']) == '00:00:00' ? ' ,1' : ' ,0').')';
    $obj_result = mysqli_query($obj_db, $str_query);

    $str_query = 'SELECT *, event_id as id, concat_ws(" ",date_start,time_start) as start,concat_ws(" ",date_end,time_end) as end ' .
            'FROM events WHERE event_id = '.mysqli_insert_id($obj_db);
    $obj_result = mysqli_query($obj_db, $str_query);
    $arr_event = mysqli_fetch_array($obj_result, MYSQLI_ASSOC);

    $arr_event['allDay']    = $arr_event['allDay'] == 0 ? false : true;
    $arr_event['editable']  = $arr_event['user_id'] == $_SESSION['user_id'] ? true : false;

    echo json_encode(array('success'=>true, 'event'=>$arr_event ));exit;

}

//function updateEventTitle() {
//  global $obj_db;

//  $str_query = 'UPDATE events SET title = "'.$_POST['title'].'" WHERE event_id = '.$_POST['event_id'].' AND user_id = '. $_SESSION['user_id'];
//  $obj_result = mysqli_query($obj_db, $str_query);

//  echo json_encode(array('success'=>true));exit;
//}

function updateEvent() {
    global $obj_db;

    $str_query = 'UPDATE events SET date_start = "'.date('Y-m-d', $_POST['date_start']).'" ' .
            (isset($_POST['title']) ? ', title = "'.$_POST['title'].'"' : '').
                (isset($_POST['color']) && $_POST['color'] != 'undefined' ? ', color = "'.$_POST['color'].'"' : '').
            ', date_end = "'.date('Y-m-d', $_POST['date_end']).'" ' .
            ', time_start = "'.date('H:i:s', $_POST['date_start']).'" ' .
            ', time_end = "'.date('H:i:s', $_POST['date_end']).'" ' .
            ((isset($_POST['allDay']) && $_POST['allDay']) || (date('H:i:s', $_POST['date_start']) == '00:00:00' && date('H:i:s', $_POST['date_end']) == '00:00:00') ? ' ,allDay = 1' : ' ,allDay = 0').
                ' WHERE event_id = '.$_POST['event_id'].' AND user_id = '. $_SESSION['user_id'];
    $obj_result = mysqli_query($obj_db, $str_query);


    $str_query = 'SELECT *, event_id as id, concat_ws(" ",date_start,time_start) as start,concat_ws(" ",date_end,time_end) as end ' .
            'FROM events WHERE event_id = '.$_POST['event_id'];
    $obj_result = mysqli_query($obj_db, $str_query);
    $arr_event = mysqli_fetch_array($obj_result, MYSQLI_ASSOC);

    $arr_event['allDay']    = $arr_event['allDay'] == 0 ? false : true;
    $arr_event['editable']  = $arr_event['user_id'] == $_SESSION['user_id'] ? true : false;

    echo json_encode(array('success'=>true, 'event'=>$arr_event ));exit;
}

function resizeEvent() {
    global $obj_db;

    $str_query = 'UPDATE events SET date_start = "'.date('Y-m-d', $_POST['date_start']).'" ' .
            ', date_end = "'.date('Y-m-d', $_POST['date_end']).'" ' .
            ', time_start = "'.date('H:i:s', $_POST['date_start']).'" ' .
            ', time_end = "'.date('H:i:s', $_POST['date_end']).'" ' .
                ' WHERE event_id = '.$_POST['event_id'].' AND user_id = '. $_SESSION['user_id'];
    $obj_result = mysqli_query($obj_db, $str_query);



    echo json_encode(array('success'=>true));exit;
}

function deleteEvent() {
    global $obj_db;

    $str_query = 'DELETE FROM events WHERE event_id = '.$_POST['event_id'].' AND user_id = '. $_SESSION['user_id'];
    $obj_result = mysqli_query($obj_db, $str_query);

    echo json_encode(array('success'=>true));exit;
}
?>

我的 index.php

<?php
/*
 * Created on 17-okt-2011
 * author Paul Wolbers
 */

require_once 'include/default.inc.php';

if(isset($_GET['action'])) {
    switch($_GET['action']) {
        case 'agenda':
            showAgenda();
            break;
        case 'agenda_widget':
            showAgenda(true);
            break;
        case 'timeline':
            showTimeline();
            break;
        case 'get_timeline_events':
            getTimelineEvents();
            break;

  }
} else {
    $obj_smarty->display(FULLCAL_DIR.'/view/cal.html');
}

function getDaysBetween($sStartDate, $sEndDate){
    $sStartDate     = date("Y-m-d", strtotime($sStartDate));
    $sEndDate       = date("Y-m-d", strtotime($sEndDate));
    $aDays[]        = $sStartDate;
    $sCurrentDate   = $sStartDate;

    while($sCurrentDate < $sEndDate){
        $sCurrentDate = date("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));
        $aDays[] = $sCurrentDate;
    }
    return $aDays;
}

function showAgenda($bln_widget=false) {
    global $obj_db;

    $str_query = 'SELECT * , event_id AS id, concat_ws( " ", date_start, time_start ) AS
                    START , concat_ws( " ", date_end, time_end ) AS
                    END FROM events WHERE date_start >= DATE( NOW( ) )
                    OR (
                    date_start < DATE( NOW( ) )
                    AND date_end >= DATE( NOW( ) )
                    )
                    ORDER BY date_start ';
    $obj_result = mysqli_query($obj_db, $str_query);

    while ($arr_line = mysqli_fetch_array($obj_result, MYSQLI_ASSOC)) {
        $arr_content[] = $arr_line;

    }
//print_r($arr_content);
    $arr_result = array();

    foreach($arr_content as $event) {
        // meerdaags event
        if($event['date_end'] != $event['date_start']) {

            $days_in_between = getDaysBetween($event['date_start'], $event['date_end']);
            foreach($days_in_between as $day) {
                if(strtotime($day) >= strtotime(date("Y-m-d"))) {
           $arr_result[$day][] = $event;
        }

            }


        } else {
            $arr_result[$event['date_start']][] = $event;
        }
    }
    //print_r($days_in_between);exit;
    //print_r($arr_result);

    $obj_smarty = new Smarty();
    $obj_smarty->compile_dir = 'templates_c/';

    $obj_smarty->assign('items', $arr_result);

    if($bln_widget) {
        $obj_smarty->display(FULLCAL_DIR.'/view/agenda_small.html');
    } else {
        $obj_smarty->display(FULLCAL_DIR.'/view/agenda.html');
    }
}

function showTimeline() {


    $obj_smarty = new Smarty();
    $obj_smarty->compile_dir = 'templates_c/';

    //$obj_smarty->assign('items', $arr_result);

    $obj_smarty->display(FULLCAL_DIR.'/view/timeline.html');

}

function getTimelineEvents() {
//[{{
//    "id": "jshist",
//    "title": "A little history of JavaScript",
//    "focus_date": "2001-01-01 12:00:00",
//    "initial_zoom": "43",
//    "timezone": "-07:00",
//    "events": [
//    {
//      "id": "jshist-01",
//      "title": "Mocha - Live Script",
//      "description": "JavaScript was originally developed by Brendan Eich of
//                Netscape under the name Mocha. LiveScript was the official name for the
//                language when it first shipped in beta releases of Netscape Navigator 2.0
//                in September 1995",
//      "startdate": "1995-04-01 12:00:00",
//      "enddate": "1995-04-01 12:00:00",
//      "date_display": "month",
//      "link": "http://en.wikipedia.org/wiki/JavaScript",
//      "importance": 40,
//      "icon":"square_blue.png"
//     },
//     {
//      "id": "jshist-02",
//      "title": "JavaScript is Born",
//      "description": "LiveScript is Renamed JavaScript in a joint
//                announcement with Netscape and Sun Microsystems",
//      "startdate": "1995-12-04 12:00:00",
//      "enddate": "1995-12-04",
//      "date_display": "day",
//      "link": "http: //en.wikipedia.org/wiki/JavaScript",
//      "importance": 50,
//      "icon":"triangle_orange.png"
//    }
//  ]
//  }
//]
    global $obj_db;

    $arr_content = array("id"=> "jshist",
        "title"=> "test",
        "focus_date"=> "2012-01-01 12:00:00",
        "initial_zoom"=> "20",
        "timezone"=> "+01:00"
        );

    $str_query = 'SELECT *, event_id as id, concat_ws(" ",date_start,time_start) as startdate,concat_ws(" ",date_end,time_end) as enddate FROM events ';
    $obj_result = mysqli_query($obj_db, $str_query);

    while ($arr_line = mysqli_fetch_array($obj_result, MYSQLI_ASSOC)) {
        $arr_line['importance'] = 20;
        $arr_events[] = $arr_line;

    }

    $arr_content['events'] = $arr_events;
    echo json_encode(array($arr_content));

}
?>
4

1 回答 1

0

您已经注释掉了cal_events.php连接到数据库的开头的行:

//require_once '../config/db.config.php';
//database_connect();

没有它们,addEvent()使用该$obj_db变量的函数将无法工作,因为它不会被定义。

于 2012-05-30T09:18:15.443 回答