/ PHP

php操作数据库代码经验分享

一、连接数据库

①mysql

$conn = mysql_connect("$sqlhost","$usename","$password"); $conn_b = mysql_select_db("$sqlname"); if(!$conn) { die("数据库链接失败,请检查帐号或密码是否正确!"); } if(!$conn_b) { die( "数据库打开失败,请检查数据库名称是否正确!"); }

②mssql

$conn = mssql_connect("$sqlhost","$usename","$password"); $conn_b = mssql_select_db("$sqlname"); if(!$conn) { die("数据库链接失败,请检查帐号或密码是否正确!"); } if(!$conn_b) { die( "数据库打开失败,请检查数据库名称是否正确!"); }

二、select数据

①mysql

$sql = "select * from user where usr='$mobile'"; $query = mysql_query($sql,$conn); if(0 == mysql_num_rows($query)) { echo "No data selected!"; }

②mssql

$sql = "select * from [user] where usr='$mobile'"; $query = mssql_query($sql,$conn); if(0 == mssql_num_rows($query)) { echo "No data selected!"; }

三、insert与update

①mysql

$sql = "insert reg_phone (number,code,hash,state) values ('$mobile','$code','$hash',0)"; $query = mysql_query($sql,$conn); if(mysql_rows_affected($conn)==0) { echo "Query Error"; }

②mssql

$sql = "insert [reg_phone] (number,code,hash,state) values ('$mobile','$code','$hash',0)"; $query = mssql_query($sql,$conn); if(mssql_rows_affected($conn)==0) { echo "Query Error"; }

仅仅是个人经验,欢迎吐槽。。。

php操作数据库代码经验分享
Share this