php连接后找到数据库 php连接后找到数据库怎么办( 六 )


function set_db ( $dbfile ){
$this-database = $dbfile;
}
/**
* 设置连接数据库的用户名和密码
* @param string $user 用户名
* @param string $pwd 密码
*
* @return void
*/
function set_login ( $host , $user , $pwd ){
$this-host=$host;
$this-username=$user;
$this-password=$pwd;
}
/**
* 创建数据库连接
* @param
* return void
*/
function set_conn (){
$this-conn=mysql_connect($this-host,$this-username,$this-password );
if ( isset($this-conn)mysql_select_db($this-database) )
$this-error=0;
else
$this-error=1;
}
/**
* 设置当前操作的数据表
* @param string $tb
*
* @return void
*/
function set_table( $tb ) {
$this-table = $tb;
}
/**
* 返回sql查询结果
* @param string $sql sql语句
*
* @return #id
*/
function get_result( $sql ){
return mysql_query( $sql , $this-conn );
}
/**
* 获取查询的结果
* @param string $sql
*
* @return array 结果的二维数组
*/
function get_result_rows( $sql ){
$array = array() ;
$result = $this-get_result( $sql );
while( $row = mysql_fetch_assoc( $result ) )
$array[] = $row ;
return $array;
}
/**
* 获取部分查询结果
*
* @param Array 数组
* @return Array
*/
function get_query_result( $cols , $tb=null , $condition , $order=null , $limit=null , $start=0 ) {
if( empty($tb) ) $tb=$this-table;
else $this-table=$tb;
if( is_array($cols) ) $col="`".implode('`,`',$cols)."`";
else $col = $cols;
if( isset($limit) )
$sql.="select top $limit $col from $tb";
else
$sql = "select $col from $tb";
if( isset($condition) ) $sql.=" where $condition";
if( isset($order) ) $sql.=" order by $order";
if( isset($limit) ) $sql.=" limit $start,$limit";
return $this-get_result_rows($sql);
}
/**
* 执行数据库插入操作
*
* @param $arr values列表,数组索引为数据表字段
* @param $tb 操作数据表 如果为空则为设置的当前类的操作表
*/
function insert_info( $arr , $tb = "" ) {
$cols = array_keys( $arr );
$values = array_values( $arr );
if (empty($tb)) $tb = $this-table;
/*
foreach( $arr as $key = $value ){
$cols[] = $key;
$values[] = $value;
}
*/
$sql = "insert into [$tb](`".implode("`,`",$cols)."`) values('".implode("','",$values)."')";
//return $sql;
return $this-get_result( $sql );
}
/**
* 执行数据库更新操作
*
* @param array $arr 要更新的字段值 数组索引为表字段名
* @param array $con 条件数组
* @param string $tb 要操作的数据表
*
*/
function update_info( $arr , $con , $tb = "" ) {
$cols = array();
$conditions = array();
if (empty( $tb )) $tb = $this-table;
if( is_array($arr) ) {
foreach( $arr as $key = $value ){
$cols[] = "`$key`='$value'";
}
foreach( $con as $key = $value ) {
//检查数据类型
if( is_int($value) || is_float($value) )
$conditions[] = "`$key`=$value";
else
$conditions[] = "`$key`='$value'";
}
$sql = "update `$tb` set ".implode(",",$cols)." where ".implode(" and ",$conditions);
}
else
$sql = "update `$tb` set $arr where $con";
//return $sql;
return $this-get_result( $sql );
}
}
PHP连接MySQL,怎么测试她们已经连接成功?PHP连接mysql使用mysql_connect函数即可进行与mysql数据库之间的通讯 。如果连接成功 , 则返回一个 MySQL 连接标识 , 失败则返回 FALSE 。示例如下:

推荐阅读