gpt4 book ai didi

php - 在 PHP/MySQL 连接中出现 T_CONSTANT_ENCAPSED_STRING 错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:08:21 25 4
gpt4 key购买 nike

<分区>

我正在构建一个可以简化与 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;

有什么问题吗?

编辑:谢谢。我纠正了语法错误,但它似乎给了我一个新错误: fatal error :第 13 行无法访问 C:\xampp\htdocs\test\mysql\mysql.php 中的空属性

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com