mysql生态_mysql原生态操作函数

//conf.php内容

return array(

'host'=>'localhost',

'user'=>'root',

'pwd'=>'910420',

'db'=>'boke',

'charset'=>'utf8',

'salt'=>'Lsd":Adfqef]',

);

//mysql.php内容

/**

*

*数据库的操作的封装函数

*/

/**

*

*连接数据库

*@return resource 成功返回一个资源

*/

function con(){

//静态变量使得$con在一个页面只能访问一次

static $con= null;

if($con===null){

$conf=include(ROOT.'/lib/conf.php');

$con=mysql_connect($conf['host'],$conf['user'],$conf['pwd']);

mysql_query('use '.$conf['db']);

mysql_query('set names '.$conf['charset']);

}

return $con;

}

/**

*执行sql语句

*

*@param string $sql

*@return mixed 返回布尔值/资源

*/

function mQuery($sql){

$rs=mysql_query($sql,con());

if($rs){

mLog('成功'."\n".$sql);

}else{

mLog('失败!'."\n".$sql.'\n'.mysql_error());

}

return $rs;

}

/**

*

*生成log日志

*@param string $sql

*无返回值,直接生成log文件

*/

function mLog($log){

$filename=ROOT.'/log/'.date('Ymd').'.txt';

$data='----------------------------'."\n";

$data.= date('Y/m/d H:i:s')."\n";

$data.= $log;

$data.="\n"."------------------------------"."\n";

file_put_contents($filename,$data,FILE_APPEND);

}

/**

*查询select语句并返回多行,适用于多条查询

*

*@param string $sql

*@return array 查询到返回二维关联数组,未查到返回false

*/

function getAll($sql){

$rs=mQuery($sql);

if(!$rs){

return false;

}else{

$arr=array();

while($row=mysql_fetch_assoc($rs)){

$arr[]=$row;

}

return $arr;

}

}

/**

*

*查询单条数据

*@param string $sql

*$return array 查询返回一个一维关联数组,未查到返回false

*/

function find($sql){

$rs=mQuery($sql);

if(!$rs){

return false;

}else{

$row=mysql_fetch_assoc($rs);

}

return $row;

}

/**

*

*查询select语句并返回索引数组

*@param string $sql select

*@return array string 返回一个一维索引数组,未查到返回false

*/

function getCount($sql){

$rs=mQuery($sql);

return $rs? mysql_fetch_row($rs) : false;

}

/**

*

*拼接update和insert语句,并直接进行插入更改

*@param string $table 要操作的数据表名

*@param array $data 要进行插入的数据,键为数据库字段,值为新值

*@param sting $act 选择方法,插入或者更新,默认插入

*@paran string $where 防止update时忘记加where,改了所有值

*/

function inUp($table,$data,$act='insert',$where='0'){

if($act=='insert'){

$sql="insert into ".$table." (";

$sql.=implode(',',array_keys($data)).") values ('";

$sql.=implode("','",array_values($data))."')";

//return $sql;

return mQuery($sql);

}elseif($act=='update'){

$str='';

foreach($data as $k => $v){

$str.=$k."='".$v."',";

}

$str=rtrim($str,',');

$sql="update ".$table." set ".$str." where ".$where;

return mQuery($sql);

}else{

return false;

}

}

/**

*

*返回最后一次insert产生的主键id

*@return int

*/

function lastId(){

return mysql_insert_id(con());

}

//$sql="delete from cat where cat_id='11'";

//var_dump(inUp('cat',array('id'=>1,'name'=>'ddd'),'insert'));

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值