我正在构建一个可以简化与 MySQL 连接的类,但我收到了 T_CONSTANT_ENCAPSED_STRING 错误。我相信它与字符串有关,但我没有发现任何问题。
代码如下:
<?php
class mysql
{
private $host;
private $user;
private $pass;
private $connection;
private $database;
private $result;
public function __construct()
{
$this -> $host = 'localhost';
$this -> $user = 'root';
$this -> $pass = '';
}
public function connect($_host, $_user, $_pass)
{
if (isset('$_host')) $this -> $host = $_host;
if (isset('$_user')) $this -> $user = $_user;
if (isset('$_pass')) $this -> $pass = $_pass;
$this -> disconnect();
$this -> $connection = mysql_connect($this -> $server, $this -> $user, $this -> $pass);
if (! $this -> $connection) return mysql_error();
else return true;
}
public function isconnected()
{
if(is_resource($this -> $connection) && get_resource_type($this -> $connection) === 'mysql link') return true;
else return false;
}
public function disconnect()
{
if ($this -> isconnected()) mysql_close($this -> $connection);
}
private function setdb($_dbname)
{
$this -> $database = $_dbname;
if (! mysql_select_db($this -> $database, $this -> $connection)) return mysql_error();
else return true;
}
public function runquery($_query)
{
if(! isset($database)) return mysql_error();
else return mysql_query($_query,$this -> $connection);
}
public function __destruct()
{
$this -> disconnect();
unset($this -> $host);
unset($this -> $user);
unset($this -> $pass);
unset($this -> $connection);
unset($this -> $database);
unset($this -> $result);
}
}
$phpmyadmin = new mysql();
echo $phpmyadmin.connect('localhost','root','');
echo $phpmyadmin.setdb('DBTEST');
$result = $phpmyadmin.runquery("SELECT * FROM TABTEST");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row['PERSON_NAME'];
}
?>
我得到的错误是:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\xampp\htdocs\test\mysql\mysql.php on line 20
第 20 行很明显:
if (isset('$_host')) $this -> $host = $_host;
有什么问题?
编辑:谢谢。我更正了语法错误,但它似乎给了我一个新错误:致命错误:无法访问第 13 行 C:\xampp\htdocs\test\mysql\mysql.php 中的空属性