我无法从我的 MySQLi 查询中打印出值。这是我正在使用的数据库连接类。
class db
{
public function __construct()
{
$this->mysqli = new mysqli('localhost', 'root','', 'database');
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
public function Query($SQL)
{
$this->SQL = $this->mysqli->real_escape_string($SQL);
$this->Result = $this->mysqli->query($SQL);
if ($this->Result == true)
return true;
else
die('Problem with Query: ' . $this->SQL);
}
public function Get($field = NULL)
{
if ($field == NULL)
{
$data = array();
while ($row = $this->Result->fetch_assoc())
{
$data[] = $row;
}
}
else
{
$row = $this->Result->fetch_assoc();
$data = $row[$field];
}
$this->Result->close();
return $data;
}
public function __destruct()
{
$this->mysqli->close();
}
}
运行查询
$db = new db;
$db->Query("SELECT * FROM tblclients WHERE clientid = $this->id");
$result = $db->Get();
echo $result['clientid'];
我收到错误
PHP Notice: Undefined index: clientid
但是我知道当我运行时这些值被传递给 $results 数组
print_r ($result);
我得到这个返回
Array ( [0] => Array ( [clientid] => 2 [firstname] => John [lastname] => Doe [dob] => 1962-05-08))
对于它的价值,如果我尝试echo $db->Get('firstname');
一切正常。一段时间以来我一直把头撞在墙上,任何帮助表示赞赏。