本文主要是介绍无意搜集到的一个mysql封装函数(php),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<span style="font-family: Arial, Helvetica, sans-serif;"><?php</span>
function dbConnect(){global $db;$db = DB_PCONNECT? mysql_pconnect(DB_HOST,DB_USERNAME,DB_PASSWORD): mysql_connect(DB_HOST,DB_USERNAME,DB_PASSWORD);mysql_query("SET NAMES 'UTF8'");mysql_select_db(DB_NAME,$db);
}
function dbClose(){global $db;mysql_close($db);
}
function dbExecute($sql){global $db;mysql_query($sql,$db);return mysql_affected_rows($db);
}
function dbQuery($sql){global $db;$result = mysql_query($sql,$db);while($row = mysql_fetch_assoc($result)){$data[] = $row;}return $data;
}
function dbGetRow($sql){global $db;$result = mysql_query($sql,$db);return mysql_num_rows($result)>0? mysql_fetch_assoc($result): null;
}
function dbGetField($sql, $field){$result = dbGetRow($sql);return count($result)==null? null: (isset($result[$field])? $result[$field]: null);
}
function makeInsertSql($table,$data){$t1 = $t2 = array();foreach($data as $key=>$value){$t1[] = $key;$t2[] = "'".$value."'";}//echo "insert into $table(".implode(',',$t1).") values(".implode(',',$t2).")";return "insert into $table(".implode(',',$t1).") values(".implode(',',$t2).")";
}
function makeUpdateSql($table,$data,$condition){$t1 = array();foreach($data as $key=>$value){$t1[] = "$key='".$value."'";}//echo "update $table set ".implode(',',$t1)." where $condition";return "update $table set ".implode(',',$t1)." where $condition";
}
function startTrans(){mysql_query("SET AUTOCOMMIT=0");mysql_query("BEGIN");}
function commit(){mysql_query("COMMIT");endTrans();
}
function rollback(){mysql_query("ROLLBACK");endTrans();
}
function endTrans(){mysql_query("END");mysql_query("SET AUTOCOMMIT=1");
}
function makeInsertAllSql($datas,$table){if(!is_array($datas[0])) return false;$fields = array_keys($datas[0]);$values = array();foreach ($datas as $data){$value = array();foreach ($data as $key=>$val){$val = htmlspecialchars(addslashes($val));if(is_scalar($val)) { // 过滤非标量数据$value[] = is_string($val) ? "'{$val}'" : $val;}}$values[] = '('.implode(',', $value).')';}$sql = ('INSERT').' INTO '.$table.' ('.implode(',', $fields).') VALUES '.implode(',',$values);return $sql;
}
?>
这篇关于无意搜集到的一个mysql封装函数(php)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!