20,359
社区成员
发帖
与我相关
我的任务
分享
<?
$isdebug = false;
class MysqlTool
{
public $conn;
public function __construct($server, $name, $pwd, $dbname, $charset='utf8')
{
$this->conn = mysql_connect($server, $name, $pwd);
mysql_query("use $dbname", $this->conn);
mysql_query("set character set $charset", $this->conn);
mysql_query("set names $charset", $this->conn);
}
//insert update delete
public function exe($sql)
{
global $isdebug;
$temp = mysql_query($sql, $this->conn);
$err = mysql_error();
if (empty($err) == false && $isdebug == true)
{
print( '<br /><hr style="color:green" /><font color="#0000FF">错误信息:' . mysql_error() . '</font><br /><font color="red">执行SQL:' . $sql . '</font>' );
}
else if (empty($err) == false)
{
die("system error");
}
return $temp;
}
// select *....
public function query_to_array($sql)
{
$temp_array = array();
$result = $this->exe($sql);
while ($row = mysql_fetch_array($result))
{
array_push($temp_array, $row);
}
return $temp_array;
}
// select top 1 id from tb...
public function query_single($sql)
{
$result = $this->exe($sql);
if ($row = mysql_fetch_row($result))
{
return $row[0];
}
}
function __destruct()
{
mysql_close($this->conn);
}
}
$db = new MysqlTool("localhost", "root", "", "db_dahuangfeng");
?>
这是top.php的,在引用top.php的文件里面已经include_once了这个类
<?
function select_menu($p_id,$type_class)
{
$ret_types = $db->query_to_array("select * from tb_types where p_id = $p_id and type_class = $type_class order by type_id desc;");
$html = '';
foreach($ret_types as $k => $v)
{
$html .= '<li><a href="';
if($type_class == 1)
{
$html .= 'about';
}
if($type_class == 2)
{
$html .= 'news';
}
$html .= '.php?t=' . $v['type_id'] . '">' . $v['type_name'] . '</a></li>';
}
return $html;
}
?>
$db = new MysqlTool("localhost", "root", "", "db_dahuangfeng");
在类的最下面实例化了,但是在top.php的函数里面取不到这个类的查询方法