/* public: find available table names */
function table_names() {
$this->connect();
$h = @mysql_query("show tables", $this->Link_ID);
$i = 0;
while ($info = @mysql_fetch_row($h)) {
$return[$i]["table_name"] = $info[0];
$return[$i]["tablespace_name"] = $this->Database;
$return[$i]["database"] = $this->Database;
$i++;
}
@mysql_free_result($h);
return $return;
}
/* private: error handling */
function halt($msg) {
$this->Error = @mysql_error($this->Link_ID);
$this->Errno = @mysql_errno($this->Link_ID);
if ($this->locked) {
$this->unlock();
}
if ($this->Halt_On_Error == "no")
return;
$this->haltmsg($msg);
if ($this->Halt_On_Error != "report")
die("Session halted.");
}
function haltmsg($msg) {
printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
printf("<b>MySQL Error</b>: %s (%s)<br>\n",
$this->Errno,
$this->Error);
}
//----------------------------------
// 模块: 自定义函数
// 功能: 部分实用的数据库处理方法
// 作者: heiyeluren
// 时间: 2005-12-26
//----------------------------------
/**
* 方法: execute($sql)
* 功能: 执行一条SQL语句,主要针对没有结果集返回的SQL
* 参数: $sql 需要执行的SQL语句,例如:execute("DELETE FROM table1 WHERE id = '1'")
* 返回: 更新成功返回True,失败返回False
*/
function execute($sql)
{
if (empty($sql))
{
$this->error("Invalid parameter");
}
if (!$this->query($sql))
{
return false;
}
return true;
}
/**
* 方法: get_all($sql)
* 功能: 获取SQL执行的所有记录
* 参数: $sql 需要执行的SQL,例如: get_all("SELECT * FROM Table1")
* 返回: 返回包含所有查询结果的二维数组
*/
function get_all($sql)
{
$this->query($sql);
$result_array = array();
while($this->next_record())
{
$result_array[] = $this->Record;
}
if (count($result_array)<=0)
{
return 0;
}
return $result_array;
}
/**
* 方法: get_one($sql)
* 功能: 获取SQL执行的一条记录
* 参数: $sql 需要执行的SQL,例如: get_one("SELECT * FROM Table1 WHERE id = '1'")
* 返回: 返回包含一条查询结果的一维数组
*/
function get_one($sql)
{
$this->query($sql);
if (!$this->next_record())
{
return 0;
}
return $this->Record;
}
/**
* 方法: get_limit($sql, $limit)
* 功能: 获取SQL执行的指定数量的记录
* 参数:
* $sql 需要执行的SQL,例如: SELECT * FROM Table1
* $limit 需要限制的记录数
* 例如 需要获取10条记录, get_limit("SELECT * FROM Table1", 10);
*
* 返回: 返回包含所有查询结果的二维数组
*/
function get_limit($sql, $limit)
{
$this->query($sql);
$result_array = array();
for ($i=0; $i<$limit&&$this->next_record(); $i++)
{
$result_array[] = $this->Record;
}
if (count($result_array) <= 0)
{
return 0;
}
return $result_array;
}