PHP常用数据库代码汇总

连接MYSQL

    //MYSQL数据库配置
define(DB_HOST, '127.0.0.1');
define(DB_USER, 'user');
define(DB_PASS, 'pass');
define(DB_DATABASENAME, 'database'); //连接mysql数据库
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("connect failed" . mysql_error());
mysql_select_db(DB_DATABASENAME, $conn);

连接MSSQL

     $conn=mssql_connect('IP:PORT','userid','pass');
mssql_select_db('database',$conn);
$sql="select * from dde_top";
$Result=mssql_query($sql);

SELECT语句

    //SELECT语句
$sql=sprintf("SELECT username FROM v9_member where username='%s' ",$userid);
$result=mysql_query($sql); //用途1:获取记录数量
$num = mysql_num_rows($result);
if($num==0){
echo "no_user"; //返回1: 没有此用户
return;
}
//用途2:获取一条数据
$row = mysql_fetch_array($result)
$row = mysql_fetch_array($result)
$row = mysql_fetch_array($result) //用途3:遍历记录集,并转化成数组 while( $row = mysql_fetch_array($result) ){
$game = $row['game']; //游戏名
$giftname = $row['giftname']; //礼包名字
}

UPDATE语句

     //更新此CDKEY的used,userid,getdate
$sql=sprintf("UPDATE qyg_gifts SET userid='%s',getdate=%d WHERE cdkey='%s' ",$userid,time(),$cdkey);
$result=mysql_query($sql);

DELETE语句

     $sql="delete from qyg_gifts where game='$select_game'";
mysql_query($sql);

INSERT语句

//A:插入一条数据
$sql=sprintf("INSERT INTO `t_gifts` (`F_UserID`, `F_Gift`, `F_Count`) VALUES ('%s','%s',%d)",$userid,$gift,$count);
$result=mysql_query($sql);
 //B:生成一次性插入多条的SQL语句

         $sqls="INSERT INTO `t_gifts` (`F_UserID`, `F_Gift`, `F_Count`, `F_Desc`, `F_Used`, `F_BlockNo`, `F_GetDate`) VALUES";
$i=0;
foreach($items_arr as $item){ if($i==0){
$sqls=$sqls.sprintf("('%s','%s',%d,'%s',0,-1,'%s')",$userid,$item[0],$item[1],$gift_name,date('Y-m-d H:i:s',time()) );
}else{
$sqls=$sqls.sprintf(",('%s','%s',%d,'%s',0,-1,'%s')",$userid,$item[0],$item[1],$gift_name,date('Y-m-d H:i:s',time()) );
}
$i++;
}
$sqls=$sqls.";";
$result=mysql_query($sql);

PHP常用数据库代码汇总

上一篇:Codeforces Beta Round #1 A B C


下一篇:SQL Server 2014内存优化表的使用场景