XSDatabaseMySQLI
包 | XS.util.db |
---|---|
继承关系 | class XSDatabaseMySQLI » XSDatabase |
版本 | 1.0.0 |
源代码 | sdk/php/util/XSDataSource.class.php |
面向对象的 MySQLI 扩展
Public 方法
名称 | 描述 | 定义于 |
---|---|---|
close() | 关闭数据库连接 | XSDatabaseMySQLI |
connect() | 连接数据库 | XSDatabaseMySQLI |
query() | 执行 SQL 语句查询 | XSDatabaseMySQLI |
query1() | 查询数据库首行 | XSDatabase |
setUtf8() | 将输出字符集设置为 UTF-8 | XSDatabaseMySQLI |
方法明细
close()
方法
public void close()
|
源码: sdk/php/util/XSDataSource.class.php#L574 (显示)
public function close()
{
if ($this->obj)
{
$this->obj->close();
$this->obj = null;
}
}
关闭数据库连接
connect()
方法
public void connect(array $param)
| ||
$param | array | 连接参数, 包含: user,pass,host,table,dbname ... |
源码: sdk/php/util/XSDataSource.class.php#L554 (显示)
public function connect($param)
{
$host = isset($param['host']) ? $param['host'] : ini_get('mysqli.default_host');
$user = isset($param['user']) ? $param['user'] : ini_get('mysqli.default_user');
$pass = isset($param['pass']) ? $param['pass'] : ini_get('mysqli.default_pw');
$port = isset($param['port']) ? $param['port'] : ini_get('mysqli.default_port');
$this->obj = new mysqli($host, $user, $pass, '', $port);
if ($this->obj->connect_error)
throw new XSException("Can not connect to mysql server: '$uesr@$host'");
if (!$this->obj->select_db($param['dbname']))
{
$this->close();
throw new XSException("Can not switch to database name: '{$param['dbname']}'");
}
$this->setUtf8();
}
连接数据库
query()
方法
public mixed query(string $sql)
| ||
$sql | string | 要执行的 SQL 语句 |
{return} | mixed |
源码: sdk/php/util/XSDataSource.class.php#L588 (显示)
public function query($sql)
{
//echo "[DEBUG] SQL: $sql\n";
$res = $this->obj->query($sql);
if ($res === false)
throw new XSException('MySQL ERROR(#' . $this->obj->error . '): ' . $this->obj->errno);
if (!is_object($res))
$ret = $res;
else
{
$ret = array();
while ($tmp = $res->fetch_assoc())
{
$ret[] = $tmp;
}
$res->free();
}
return $ret;
}
执行 SQL 语句查询
setUtf8()
方法
public bool setUtf8()
| ||
{return} | bool | 始终返回 true |
源码: sdk/php/util/XSDataSource.class.php#L612 (显示)
public function setUtf8()
{
$this->obj->set_charset('utf8');
return true;
}
将输出字符集设置为 UTF-8