php封装一个mysql的类_一介简介的Mysql封装类_php

//######################################################################

//##### TITLE       :: CLASS MYSQL

//##### FILE        :: class_mysql.php

//##### PROJECT     :: webVision

//##### RELATED DOCUMENT :: None

//##### DESCRIPTION   ::

http://www.gaodaima.com/46779.html一介简介的Mysql封装类_php

//#####     To provide access utility for MySQL access

//#####     RunDB is used to run SQL query with the result

//#####     grouped into array.

//##### AUTHOR      :: Mark Quah

//##### REVISION  ::

//######################################################################

class MYSQL

{

var $no_rows=0, $row=array();

var $no_fields=0, $field=array();

//#-----------------------------------------------------------------

//#---- FUNCTION :: MYSQL($p_host, $p_user, $p_passwd, $p_db="mysql")

//#---- DESCRIPTION  ::

//#----      Initialize class with information to access server

//#----      No connection will be made at this point.

//#---- INPUT ::

//#----      p_host      : server hostname|IP address

//#----      p_user      : user name to log into server

//#----      p_passwd    : passwd for the user

//#----      p_db        : database to be used

//#---- OUTPUT ::

//#----      none

//#-----------------------------------------------------------------

function MYSQL($p_host, $p_user, $p_passwd, $p_db="mysql")

{

$this->sql_host = $p_host;

$this->sql_user= $p_user;

$this->sql_passwd = $p_passwd;

$this->sql_db = $p_db;

} // end MYSQL

//#-----------------------------------------------------------------

//#---- FUNCTION :: RunDB($statement, $exp_result = "")

//#---- DESCRIPTION ::

//#----      Execute a MySQL statement in a non-persistant mode

//#---- INPUT    ::

//#----      p_statement : statement to be executed

//#----      exp_result  : is result expected?

//#----           value 1 (default): result stored in row array

//#----           value 0: result not stored in row array

//#---- OUTPUT   ::

//#----      return "OK"        : succesful

//#----      return err_message from mysql_connect

//#----      exp_result==1: additional result stored into array row

//#----          no_row contains no. of record retrieved

//#----         row[recno][ "field" ] contains value of recno record

//#----          field["fieldname"] contains the field list

//#-----------------------------------------------------------------

function RunDB($p_statement, $exp_result = 1)

{

//--- Connect to the Database

$link=mysql_connect($this->sql_host, $this->sql_user, $this->sql_passwd);

if (!$link)

return sprintf("error connecting to host %s, by user %s",

$this->sql_host, $this->sql_user) ;

//--- Select the Database

if (!mysql_select_db($this->sql_db, $link))

{   $err_msg=sprintf("Error in selecting %s database",

$this->sql_db);

$err_msg .= sprintf("error:%d %s", mysql_errno($link),

mysql_error($link));

return $err_msg;

}

//--- Execute the Statement

if (!($this->result=mysql_query($p_statement, $link)))

{   $err_msg=sprintf("Error in selecting %s database/n",

$this->sqldb);

$err_msg .= sprintf("/terror:%d/t/nerror message %s",

mysql_errno($link), mysql_error($link));

return $err_msg;

}

//--- Organize the result

if ($exp_result == 1)

{   $this->no_rows = mysql_num_rows($this->result);

$this->GroupResult();

}

//--- SUCCESS RETURN

return OK;

} // end function RunDB

//#-----------------------------------------------------------------

//#---- FUNCTION :: GroupResult( )

//#---- DESCRIPTION ::

//#----      To group the raw result retrieved in an associative array

//#----      A query has to be made using RunDB prior to this execution

//#----      The handle is storedin $result

//#---- INPUT    :: None

//#---- OUTPUT   :

//#----      return none

//#----      additional result stored into array

//#----          no_row, row[recno]["field"] = value

//#----          no_field, field["fieldname"]

//#-----------------------------------------------------------------

function GroupResult()

{

//--- Get RESULT

$is_header = FALSE;

for ( $recno = 0; $recno < $this->no_rows; $recno ++ )

{   $row = mysql_fetch_object($this->result);

//--- Get Field List

if ( ! $is_header )

{   $no_fields = 0;

$t_row = $row;

while ( $item = each($t_row) )

{   $this->field[$no_fields] = $item["key"];

$no_fields ++;

}

$this->no_fields = $no_fields;

$is_header = TRUE;

}

//---- GET DATA

while ( $item = each($row))

$this->row[$recno][$item["key"]] = $item["value"];

}

//--- END CONNECTION

mysql_free_result($this->result);

} // GroupResult

//#-----------------------------------------------------------------

//#---- FUNCTION :: ShowHTML($p_table="", $p_header = "", $p_cell = "")

//#---- DESCRIPTION ::

//#----      To return the result in HTML Table format

//#---- INPUT    ::

//#----      p_table    : HTML

//#----      p_header   : First row format

//#----      p_cell     : Individual cell format

//#---- OUTPUT   ::

//#----      "OK"        : succesful

//#----      err_message from mysql_connect

//#-----------------------------------------------------------------

function ShowHTML($p_table="", $p_header="", $p_cell="" )

{

//--- DEFAULT OPTION

$p_table=($p_table=="")?"BGCOLOR=#BB9999 BORDER=1": $p_table;

$p_header=($p_header=="")? "BGCOLOR=#9999BB" : $p_header;

$p_cell=($p_cell=="")?"BGCOLOR=#99BB99":$p_cell;

//--- DISPLAY TABLE

echo "

//--- DISPLAY HEADER LINE

echo "

";

echo "

recno";

for ($i = 0; $i < $this->no_fields; $i ++)

printf("

%s", $this->field[$i]);

//--- DISPLAY DATA

for ( $i = 0; $i < $this->no_rows; $i ++)

{   echo "

";

printf("

%-3s", $i);

for ($f = 0; $f < $this->no_fields; $f ++)

{   $f_name = $this->field[$f];

$f_value = $this->row[$i][$f_name];

if ( $f_value=="" )

$f_value=" ";

printf("

%s", $f_value);

}

}

//--- THE END

echo "

";

} // ShowHTML

} // end class MYSQL

?>

例子:

include("class_mysql.php");

//===== set up sql connection

$mysql=new MYSQL("server", "mysql userid", "mysql passwd", "mysql DB");

//==== Extract Result

$status = $mysql->RunDB("select * from user;");

if ($status != "OK")

{   echo "


DB Error: $status.
";

die;

}

for ($i = 0; $i < $mysql->no_rows; $i ++)

{

echo "Record No: " . ($i + 1) ."


";

for ($j = 0; $j < $mysql->no_fields; $j ++)

{

$field_name = $mysql->field[$j];

echo "Field: ".$field_name."  ----- Value: ".

$mysql->row[$i][$field_name]."
";

}

}

//==== Use the built-in ShowHTML format

$status = $mysql->RunDB("select * from user;");

if ($status != "OK")

{   echo "


DB Error: $status.
";

die;

}

$mysql->ShowHTML("","","CENTER");

//==== Run some query not expecting results

$stmt = ("FILL IN YOUR STAEMENT eg. INSERT INTO");

$status = $myql->RunDB($stmt, 0);

if ($status

if ($status != "OK")

{   echo "


DB Fail: $status.
";

die;

}

else

{   echo "


Success: $status.
";

die;

}

?>

欢迎大家阅读《一介简介的Mysql封装类_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

原创文章,转载请注明: 转载自搞代码

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值