包 | XS.util.db |
---|---|
继承关系 | class XSDatabaseMySQLI » XSDatabase |
版本 | 1.0.0 |
源代码 | sdk/php/util/XSDataSource.class.php |
名称 | 描述 | 定义于 |
---|---|---|
close() | 关闭数据库连接 | XSDatabaseMySQLI |
connect() | 连接数据库 | XSDatabaseMySQLI |
query() | 执行 SQL 语句查询 | XSDatabaseMySQLI |
query1() | 查询数据库首行 | XSDatabase |
setUtf8() | 将输出字符集设置为 UTF-8 | XSDatabaseMySQLI |
public void close()
|
public function close()
{
if ($this->obj) {
$this->obj->close();
$this->obj = null;
}
}
关闭数据库连接
public void connect(array $param)
| ||
$param | array | 连接参数, 包含: user,pass,host,table,dbname ... |
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: '$user@$host'");
}
if (!$this->obj->select_db($param['dbname'])) {
$this->close();
throw new XSException("Can not switch to database name: '{$param['dbname']}'");
}
$this->setUtf8();
}
连接数据库
public mixed query(string $sql)
| ||
$sql | string | 要执行的 SQL 语句 |
{return} | mixed |
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 语句查询
public bool setUtf8()
| ||
{return} | bool | 始终返回 true |
public function setUtf8()
{
$this->obj->set_charset('utf8');
return true;
}
将输出字符集设置为 UTF-8
包 | XS.util.db |
---|---|
继承关系 | class XSDatabaseMySQLI » XSDatabase |
版本 | 1.0.0 |
源代码 | sdk/php/util/XSDataSource.class.php |
名称 | 描述 | 定义于 |
---|---|---|
close() | 关闭数据库连接 | XSDatabaseMySQLI |
connect() | 连接数据库 | XSDatabaseMySQLI |
query() | 执行 SQL 语句查询 | XSDatabaseMySQLI |
query1() | 查询数据库首行 | XSDatabase |
setUtf8() | 将输出字符集设置为 UTF-8 | XSDatabaseMySQLI |
public void close()
|
public function close()
{
if ($this->obj) {
$this->obj->close();
$this->obj = null;
}
}
关闭数据库连接
public void connect(array $param)
| ||
$param | array | 连接参数, 包含: user,pass,host,table,dbname ... |
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: '$user@$host'");
}
if (!$this->obj->select_db($param['dbname'])) {
$this->close();
throw new XSException("Can not switch to database name: '{$param['dbname']}'");
}
$this->setUtf8();
}
连接数据库
public mixed query(string $sql)
| ||
$sql | string | 要执行的 SQL 语句 |
{return} | mixed |
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 语句查询
public bool setUtf8()
| ||
{return} | bool | 始终返回 true |
public function setUtf8()
{
$this->obj->set_charset('utf8');
return true;
}
将输出字符集设置为 UTF-8
留下一条评论吧!
请到论坛 登录 后刷新本页面!