下一篇 » « 上一篇

MySQL用于PHP的库, 对数据库进行操作

作者:爱好者    时间:2008-01-22    来源:php之家    点击:1545    本文共1篇文章 字体:[ ]

MySQL用于PHP的库, 对数据库进行操作

<?
/*
* Utility routines for MySQL.
*/

 

www.444p.com

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
*/ 本文来自 www.444p.com

function Setup ($user, $pass) {
$this->user = $user;
$this->pass = $pass;
}

www.444p.com版权所有

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);
}

本文来自 www.444p.com

function SelectDB ($db) {
@mysql_select_db($db, $this->id) or
MySQL_ErrorMsg ("Unable to select database: $db");
} www.444p.com版权所有

# 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);
}

本文来自 www.444p.com

# 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]);
} www.444p.com

# 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);
}

www.444p.com版权所有

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");
}

www.444p.com版权所有

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);
}

php学习之家http://www.444p.com

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);
} 本文来自 www.444p.com

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);
}
} www.444p.com

/* ********************************************************************
* MySQL_ErrorMsg
*
* Print out an MySQL error message
*
*/ php学习之家http://www.444p.com

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"); www.444p.com php学习之家

# Display the error message
$text = "<font color="#ff0000" size=+2><p>Error: $msg :";
$text .= mysql_error();
$text .= "</font>n";
die($text);
}
?> php学习之家http://www.444p.com

原作者:不详
来源:中国PHP自由联盟

www.444p.com php学习之家

 

www.444p.com

发表评论
密码: (游客不需要密码)
记住我【Alt+S 或 Ctrl+Enter 快速提交】

搜索工具


《PHP与MYsql》点击排行