全局变量在php类中不起作用

时间:2015-08-16 16:06:15

标签: php mysql oop

class Database
{
    public $connect = "";

    public function connect()
    {
       $connect = new mysqli("localhost", "root", "1521", "phone");
       ini_set('default_charset', "UTF-8");
       mysqli_set_charset($connect, "utf8");
       header('Content-type: text/html; charset=UTF-8');
       if ($connect->connect_error) {
           die('Not connect' . mysqli_connect_error());
       }
    }

    public function insert()
    {
        $sql = "SELECT id FROM personal ORDER BY id DESC LIMIT 1";
        //global $connect;
        $result = $connect->query($sql);//// this here !!!!!!!!
        $row = $result->fetch_assoc();
        $tempint = (int)$row['id'];
        $tempint += 1;
    }
}

为什么$ insert中的insert函数不起作用? 我添加了全球befor $ connect但仍然没有工作。 这个非常简单的MySQL类。

1 个答案:

答案 0 :(得分:2)

您的语法不正确。

改变这一点,

select PROC_PD_CD
from PS_PROC_PD
where PD_STRT_DT = trunc(sysdate);

To,

select PROC_PD_CD
from PS_PROC_PD
where PD_STRT_DT >= trunc(sysdate) and
      PD_STRT_DT < trunc(sysdate + 1);

此外,

$connect = new mysqli("localhost", "root", "1521", "phone");

要,

$this->connect = new mysqli("localhost", "root", "1521", "phone");

通过上述更改,它应解决问题。

$result = $connect->query($sql); 已在类的范围内创建,因此当您引用它时,您需要使用$result = $this->connect->query($sql);