包 | XS.util.db |
---|---|
继承关系 | class XSDatabaseMySQL » XSDatabase |
版本 | 1.0.0 |
源代码 | sdk/php/util/XSDataSource.class.php |
名称 | 描述 | 定义于 |
---|---|---|
close() | 关闭数据库连接 | XSDatabaseMySQL |
connect() | 连接数据库 | XSDatabaseMySQL |
query() | 执行 SQL 语句查询 | XSDatabaseMySQL |
query1() | 查询数据库首行 | XSDatabase |
setUtf8() | 将输出字符集设置为 UTF-8 | XSDatabaseMySQL |
public void close()
|
public function close()
{
if ($this->link) {
mysql_close($this->link);
$this->link = 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('mysql.default_host');
$host .= (isset($param['port']) && $param['port'] != 3306) ? ':' . $param['port'] : '';
$user = isset($param['user']) ? $param['user'] : ini_get('mysql.default_user');
$pass = isset($param['pass']) ? $param['pass'] : ini_get('mysql.default_pw');
if (($this->link = mysql_connect($host, $user, $pass)) === false) {
throw new XSException("Can not connect to mysql server: '$user@$host'");
}
if (!mysql_select_db($param['dbname'], $this->link)) {
$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 = mysql_query($sql, $this->link);
if ($res === false) {
throw new XSException('MySQL ERROR(#' . mysql_errno($this->link) . '): ' . mysql_error($this->link));
}
if (!is_resource($res)) {
$ret = $res;
} else {
$ret = array();
while ($tmp = mysql_fetch_assoc($res)) {
$ret[] = $tmp;
}
mysql_free_result($res);
}
return $ret;
}
执行 SQL 语句查询
public bool setUtf8()
| ||
{return} | bool | MySQL 自 4.1.0 起支持字符集 |
public function setUtf8()
{
if (version_compare(mysql_get_server_info($this->link), '4.1.0', '>=')) {
return @mysql_query("SET NAMES utf8", $this->link);
}
return false;
}
将输出字符集设置为 UTF-8
包 | XS.util.db |
---|---|
继承关系 | class XSDatabaseMySQL » XSDatabase |
版本 | 1.0.0 |
源代码 | sdk/php/util/XSDataSource.class.php |
名称 | 描述 | 定义于 |
---|---|---|
close() | 关闭数据库连接 | XSDatabaseMySQL |
connect() | 连接数据库 | XSDatabaseMySQL |
query() | 执行 SQL 语句查询 | XSDatabaseMySQL |
query1() | 查询数据库首行 | XSDatabase |
setUtf8() | 将输出字符集设置为 UTF-8 | XSDatabaseMySQL |
public void close()
|
public function close()
{
if ($this->link) {
mysql_close($this->link);
$this->link = 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('mysql.default_host');
$host .= (isset($param['port']) && $param['port'] != 3306) ? ':' . $param['port'] : '';
$user = isset($param['user']) ? $param['user'] : ini_get('mysql.default_user');
$pass = isset($param['pass']) ? $param['pass'] : ini_get('mysql.default_pw');
if (($this->link = mysql_connect($host, $user, $pass)) === false) {
throw new XSException("Can not connect to mysql server: '$user@$host'");
}
if (!mysql_select_db($param['dbname'], $this->link)) {
$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 = mysql_query($sql, $this->link);
if ($res === false) {
throw new XSException('MySQL ERROR(#' . mysql_errno($this->link) . '): ' . mysql_error($this->link));
}
if (!is_resource($res)) {
$ret = $res;
} else {
$ret = array();
while ($tmp = mysql_fetch_assoc($res)) {
$ret[] = $tmp;
}
mysql_free_result($res);
}
return $ret;
}
执行 SQL 语句查询
public bool setUtf8()
| ||
{return} | bool | MySQL 自 4.1.0 起支持字符集 |
public function setUtf8()
{
if (version_compare(mysql_get_server_info($this->link), '4.1.0', '>=')) {
return @mysql_query("SET NAMES utf8", $this->link);
}
return false;
}
将输出字符集设置为 UTF-8
2条评论!
没有一个具体的示例,怎么连接没MySQL穿件索引都找不到,文档全部写的不明不白的
XSDatabaseMySQL有没有demo?
请到论坛 登录 后刷新本页面!