OO PHP MySQL连接

时间:2014-03-24 17:22:41

标签: php mysql oop

我是PHP语言的新手,事实上,本周开始研究它。

尝试将某些文件连接到SQL数据库时,出现以下错误消息:

  

解析错误:语法错误,意外T_VARIABLE,期待第56行/home/cpapalardo/wwwroot/Calendario/Calendar.php中的T_FUNCTION

第56行将是:

$result = mysql_query("SELECT event_id, event_title FROM event");

我不知道我做错了什么。有人可以帮我解决这个问题吗?

提前致谢。

以下是代码:

<?php include("DB_Connect.php");

class Calendar1 extends DB_Connect{

private $data;
private $month;
private $year;
private $daysMonth;
private $startDay;

public function __construct($dbo=NULL, $data=NULL){
    parent::__construct();

    if(isset($data)){
        $this->data = $data;
    }else{
        $this->data = date('Y-m-d H:i:s');              
    }

    //convert to timestamp and determines month and year of the calendar
    $ts = strtotime($this->data);
    $this->month= date("m", $ts);
    $this->year= date('Y', $ts);            

    //how many days the month has
    $this->daysMonth= date('t', strtotime($year.'-'.($month-1).'-01'));

    //what day of the week the month starts in
    $ts = mktime(0, 0, 0, $this->month, 1, $this->year);
    $this->startDay= date('w', $ts);
}

private function loadEventData($id=NULL){
    $sql = "SELECT 
                'event_id', 'event_title', 'event_desc', 'event_start', 'event_end' 
            FROM 'events'";

    if(!empty($id)){
        $sql .="WHERE 'event_id'=:id LIMIT 1";//returns one result
    }else{
        $start_ts = mktime(0, 0, 0, $this->month, 1, $this->year);
        $end_ts = mktime(23, 59, 59, $this->month+ 1, 0, $this->year);
        $start_date = date('Y-m-d H:i:s', $start_ts);
        $end_date = date('Y-m-d H:i:s', $end_ts);

        $sql = "WHERE 'event_start'
                BETWEEN '$start_date'
                AND '$end_date'
                ORDER BY 'event_start'";
    }

}
}

//execute the SQL query and return records
$result = mysql_query("SELECT event_id, event_title FROM event");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "ID:".$row['event_id']." Name:".$row['event_title'];

}

?>



<?php

class DB_Connect{
    protected $cn;

    public function __construct(){
        $cn = mysql_connect("testDatabase", "admin", "pass123");
        mysql_select_db("admin", $cn);
    }
}
?>

2 个答案:

答案 0 :(得分:1)

您可以使用[]括号访问数组元素,而不是{}。您需要更改以下行:

echo "ID:".$row{'event_id'}." Name:".$row{'event_title'};

进入以下:

echo "ID:".$row['event_id']." Name:".$row['event_title'];

答案 1 :(得分:1)

修改

您正在为列和表使用单引号。删除它们或使用反引号。

$sql = "SELECT 
        'event_id', 'event_title', 'event_desc', 'event_start', 'event_end' 
    FROM 'events'";

应该是:

$sql = "SELECT 
            event_id, event_title, event_desc, event_start, event_end 
        FROM events";

此行$sql .="WHERE 'event_id'=:id LIMIT 1";//returns one result使用:id这是一种绑定方法,但未在您的代码中实现。

您可能需要将其更改为$sql .="WHERE event_id=$id LIMIT 1";//returns one result

event_id周围没有引号,因为它是一列。

新重写:

<?php include("DB_Connect.php");

class Calendar1 extends DB_Connect{

private $data;
private $month;
private $year;
private $daysMonth;
private $startDay;

public function __construct($dbo=NULL, $data=NULL){
    parent::__construct();

    if(isset($data)){
        $this->data = $data;
    }else{
        $this->data = date('Y-m-d H:i:s');              
    }

    //convert to timestamp and determines month and year of the calendar
    $ts = strtotime($this->data);
    $this->month= date("m", $ts);
    $this->year= date('Y', $ts);            

    //how many days the month has
    $this->daysMonth= date('t', strtotime($year.'-'.($month-1).'-01'));

    //what day of the week the month starts in
    $ts = mktime(0, 0, 0, $this->month, 1, $this->year);
    $this->startDay= date('w', $ts);
}

private function loadEventData($id=NULL){
    $sql = "SELECT 
                event_id, event_title, event_desc, event_start, event_end 
            FROM events";

    if(!empty($id)){
        $sql .="WHERE event_id=$id LIMIT 1";//returns one result
    }else{
        $start_ts = mktime(0, 0, 0, $this->month, 1, $this->year);
        $end_ts = mktime(23, 59, 59, $this->month+ 1, 0, $this->year);
        $start_date = date('Y-m-d H:i:s', $start_ts);
        $end_date = date('Y-m-d H:i:s', $end_ts);

        $sql = "WHERE 'event_start'
                BETWEEN '$start_date'
                AND '$end_date'
                ORDER BY event_start";
    }

}
}

//execute the SQL query and return records
$result = mysql_query("SELECT event_id, event_title FROM events");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "ID:".$row['event_id']." Name:".$row['event_title'];

}

?>



<?php

class DB_Connect{
    protected $cn;

    public function __construct(){
        $cn = mysql_connect("testDatabase", "admin", "pass123");
        mysql_select_db("admin", $cn);
    }
}
?>

原始答案

你的第一个代码体中的大括号似乎不匹配,不管怎么说都不在我的IDE中。

尝试删除上一个while循环后的最后一个大括号,然后将其移到上方 //execute the SQL query and return records

我已在下面对其进行了修改,供您尝试。

现在,这一行:

/*$evento = mysql_query("select * from event");

很难说是否要将其注释掉,并且会导致错误。

如果是,请删除/*或修改为/*$evento = mysql_query("select * from event"); */

我还使用/合并了mesutozer's这一行的答案(信用

echo "ID:".$row{'event_id'}." Name:".$row{'event_title'};

更改为:

echo "ID:".$row['event_id']." Name:".$row['event_title'];

此行ORDER BY 'event_start'不应包含在单引号中。

使用

ORDER BY event_start

ORDER BY `event_start`

重写:

<?php
include("DB_Connect.php");

class Calendar extends DB_Connect{

    private $data;
    private $month;
    private $year;
    private $daysMonth;
    private $startDay;

    public function __construct($dbo=NULL, $data=NULL){
        parent::__construct();

        if(isset($data)){
            $this->data = $data;
        }else{
            $this->data = date('Y-m-d H:i:s');              
        }

        //convert to timestamp and determines month and year of the calendar
        $ts = strtotime($this->data);
        $this->month= date("m", $ts);
        $this->year= date('Y', $ts);            

        //how many days the month has
        $this->daysMonth= date('t', strtotime($year.'-'.($month-1).'-01'));

        //what day of the week the month starts in
        $ts = mktime(0, 0, 0, $this->month, 1, $this->year);
        $this->startDay= date('w', $ts);
    }

    private function loadEventData($id=NULL){
        $sql = "SELECT 
                    'event_id', 'event_title', 'event_desc', 'event_start', 'event_end' 
                FROM 'events'";

        if(!empty($id)){
            $sql .="WHERE 'event_id'=:id LIMIT 1";//returns one result
        }else{
            $start_ts = mktime(0, 0, 0, $this->month, 1, $this->year);
            $end_ts = mktime(23, 59, 59, $this->month+ 1, 0, $this->year);
            $start_date = date('Y-m-d H:i:s', $start_ts);
            $end_date = date('Y-m-d H:i:s', $end_ts);

            $sql = "WHERE 'event_start'
                    BETWEEN '$start_date'
                    AND '$end_date'
                    ORDER BY `event_start`";
        }

    }

}

    //execute the SQL query and return records
    $result = mysql_query("SELECT event_id, event_title FROM event");
    //fetch tha data from the database
    while ($row = mysql_fetch_array($result)) {

    echo "ID:".$row['event_id']." Name:".$row['event_title'];

    }

    $evento = mysql_query("select * from event");

    while ($row = mysql_fetch_array ($evento)){
        echo $row['event_id'] . "<br>" . $row['event_title'] . "<br>";
    }

?>