i create a class to access mysql,(named mysql.inc.php)
but it display nothing
when i use this class create a test page which named test.php
please help me
the follow is class code
<?php
/************************
mysql database driver
create by ice_berg16
at 2003.12.10
************************/
class db_mysql
{
var $host = "";
var $user = "";
var $pwd = "";
var $db_name = "";
var $link_id = 0;
var $query_id = 0;
var $errno = 0;
var $error = "";
var $record = array();
//function list
/* constructor
*/
function db_mysql()
{
$this->host = "localhost:3306";
$this->user = "root";
$this->pwd = "format";
$this->db_name = "mysite";
}
/********function connect************
*********return 0 :error ************
*********return link_id :ok ********/
function connect($host, $user, $pwd, $db_name)
{
if("" == $host)
$host = $this->host;
if("" == $user)
$user = $this->user;
if("" == $pwd)
$pwd = $this->pwd;
if("" == $db_name)
$db_name = $this->db_name;
//now connect to the database
$this->link_id = mysql_connect($host, $user, $pwd);
if(!$this->link_id)
{
$this->halt();
return 0;
}
if(!mysql_select_db($db_name, $this->link_id))
{
$this->halt();
return 0;
}
return $this->link_id;
}
/****function query************
*****return 0 : error**********
******************************/
function query($sql)
{
if(!$this->link_id)
{
$this->halt("no connect is active.");
return 0;
}
if("" == $sql)
{
$this->halt("the query string is empty.");
return 0;
}
$this->query_id = mysql_query($sql, $this->link_id);
if(!$this->query_id)
{
$this->halt();
return 0;
}
return $this->query_id;
}
/****function get_record***
*****return 0 : error******
*****return one record****/
function get_record()
{
if(!$this->link_id || !$this ->query_id)
{
$this->halt("no query record.");
return 0;
}
$this->record = mysql_fetch_array($this->query_id);
if(!$this->record)
{
$this->halt("get record failed.");
return 0;
}
return $this->record;
}
/****function get_data()********
*****return 0 : error***********
*****return data in the record*/
function get_data($id)
{
return $this->record[$id];
}
/***fucntion total_record()***
****get the number of the*****
****record********************/
function total_record()
{
return mysql_num_rows($this->query_id);
}
/* if the operate is success
** can be used afer insert,update,delete etc.
** can not use when query is "select"
*/
function success()
{
if (mysql_affected_rows($this->link_id))
return 1;
else
return 0;
}
/* close the connect to the mysql server
*/
function close()
{
mysql_close();
}
/****function halt*********
*****process all error*****
*****and echo the error***/
function halt($err_msg="")
{
if("" == $err_msg)
{
$this->errno = mysql_errno();
$this->error = mysql_error();
echo "mysql error:\n";
echo $this->errno.":".$this->error;
}
else
{
echo "mysql error:\n";
echo $err_msg;
}
}
}
?>
//***the follow is the test page************//
<?php
require("class/mysql.inc.php");
var $db = new db_mysql();
if(!$db->connect())
echo "error: connect";
$sql="select * from printer";
if(!$db->query($sql))
echo "error: query";
while($db->get_record())
{
echo $db->get_data("id")."<br>";
echo $db->get_data("brand")."<br>";
}
?>