【参加讨论】
<?
/*
* utility routines for mysql.
*/ class mysql_class {
var $db, $id, $result, $rows, $data, $a_rows;
var $user, $pass, $host;
/* make sure you change the username and password to your name and
* password for the db
*/
function setup ($user, $pass) {
$this->user = $user;
$this->pass = $pass;
}
function create ($db) {
if (!$this->user) {
$this->user = "username"; /* 在这里作修改 */
}
if (!$this->pass) {
$this->pass = "password"; /* 在这里作修改 */
}
$this->db = $db;
$this->id = @mysql_pconnect($this->host, $this->user, $this->pass) or
mysql_errormsg("unable to connect to mysql server: $this->host : '$server_name'");
$this->selectdb($db);
}
function selectdb ($db) {
@mysql_select_db($db, $this->id) or
mysql_errormsg ("unable to select database: $db");
}
# use this function is the query will return multiple rows. use the fetch
# routine to loop through those rows.
function query ($query) {
$this->result = @mysql_query($query, $this->id) or
mysql_errormsg ("unable to perform query: $query");
$this->rows = @mysql_num_rows($this->result);
$this->a_rows = @mysql_affected_rows($this->result);
}
# use this function if the query will only return a
# single data element.
function queryitem ($query) {
$this->result = @mysql_query($query, $this->id) or
mysql_errormsg ("unable to perform query: $query");
$this->rows = @mysql_num_rows($this->result);
$this->a_rows = @mysql_affected_rows($this->result);
$this->data = @mysql_fetch_array($this->result) or
mysql_errormsg ("unable to fetch data from query: $query");
return($this->data[0]);
}
# this function is useful if the query will only return a
# single row.
function queryrow ($query) {
$this->result = @mysql_query($query, $this->id) or
mysql_errormsg ("unable to perform query: $query");
$this->rows = @mysql_num_rows($this->result);
$this->a_rows = @mysql_affected_rows($this->result);
$this->data = @mysql_fetch_array($this->result) or
mysql_errormsg ("unable to fetch data from query: $query");
return($this->data);
}
function fetch ($row) {
@mysql_data_seek($this->result, $row) or
mysql_errormsg ("unable to seek data row: $row");
$this->data = @mysql_fetch_array($this->result) or
mysql_errormsg ("unable to fetch row: $row");
}
function insert ($query) {
$this->result = @mysql_query($query, $this->id) or
mysql_errormsg ("unable to perform insert: $query");
$this->a_rows = @mysql_affected_rows($this->result);
}
function update ($query) {
$this->result = @mysql_query($query, $this->id) or
mysql_errormsg ("unable to perform update: $query");
$this->a_rows = @mysql_affected_rows($this->result);
}
function delete ($query) {
$this->result = @mysql_query($query, $this->id) or
mysql_errormsg ("unable to perform delete: $query");
$this->a_rows = @mysql_affected_rows($this->result);
}
}
/* ********************************************************************
* mysql_errormsg
*
* print out an mysql error message
*
*/
function mysql_errormsg ($msg) {
# close out a bunch of html constructs which might prevent
# the html page from displaying the error text.
echo("</ul></dl></ol>n");
echo("</table></script>n");
# display the error message
$text = "<font color="#ff0000" size=+2><p>error: $msg :";
$text .= mysql_error();
$text .= "</font>n";
die($text);
}
?>