Thick PU Education 1718 database connection job answer, split a function that operates a database without writing SQL statements

Source: Internet
Author: User
Tags explode rtrim truncated

<?php
Header ("Content-type:text/html;charset=utf8");
Functions of PHP Operations database
function Phpsql ($dbconfig, $type, $field = ", $data =array (), $condition =array (), $join =array ()) {
Determine that the database connection host does not exist
if (!isset ($dbconfig [' Host '])) {
Return "Database connection host does not exist";
}
Determine if the database user name does not exist
if (!isset ($dbconfig [' user ')]) {
Return "No database user name exists";
}
Determine if the database user name password does not exist
if (!isset ($dbconfig [' Pass ')]) {
Return "There is no database user name password";
}
Determine if there is a database that needs to be used
if (!isset ($dbconfig [' dbname '])) {
Return "There is no database to use";
}
Determine if there is no data table to use
if (!isset ($dbconfig [' table ')]) {
Return "There is no data table to use";
}
Define a SQL statement that needs to be generated
$sql = ";
if ($type = = ' Insert ') {//Add data
Connecting SQL statements
$sql. = ' INSERT INTO '. $dbconfig [' table '];
Determine the data type of field, which can be a string or an array
$fiedltype =gettype ($field);
if ($fiedltype! = ' string ' && $fiedltype! = ' array ') {
Return "field data type must be a string or array";
}elseif ($fiedltype = = ' array ') {//When the field data type is an array
$fields =implode (', ', $field);
$sql. = ' ($fields. ') values ';
}else{
$sql. = ' ($field. ') values ';
$field =explode (', ', $field);//convert the field data into an array, allowing data to be taken from $data when inserting data below
}
Determines that the inserted data cannot be empty and must be an array data type
if (!is_array ($data) | | Empty ($data)) {
Return "Data cannot be empty when inserting data, and the data type must be an array of fields = = data value format";
}
Determine the array dimension,
$tmp =isset ($data [$field [0]]) $data [$field [0]]:array ();
if (Is_array ($tmp)) {//If the first-level child element is an array, it means inserting multiple data
foreach ($data as $val) {//traversal array
$sql. = ' (';
foreach ($field as $v) {//Traversal fields, extracting data in field order
$sql. = ' "'. $val [$v]. '", ';
}
$sql =rtrim ($sql, ', ');//Remove the extra comma from the far right
$sql. = '), ';
}
$sql =rtrim ($sql, ', ');
}else{//Insert a line otherwise
$sql. = ' (';
foreach ($field as $v) {//Traversal fields, extracting data in field order
$sql. = ' "'. $data [$v]. '", ';
}
$sql =rtrim ($sql, ', ');//Remove the extra comma from the far right
$sql. = ') ';
}
Echo $sql;
}elseif ($type = = ' Update ') {//Update data
Structure of the Write UPDATE statement
$sql. = ' Update '. $dbconfig [' table ']. ' Set ';
Determine the data type of field, which can be a string or an array
$fiedltype =gettype ($field);
if ($fiedltype! = ' string ' && $fiedltype! = ' array ') {
Return "field data type must be a string or array";
}elseif ($fiedltype = = ' array ') {//When the field data type is an array
foreach ($field as $v) {
$sql. = $v. ' = '. $data [$v]. ' ", ';
}
}else{
$field =explode (', ', $field);//convert the field data into an array, allowing data to be taken from $data when inserting data below
foreach ($field as $v) {
$sql. = $v. ' = '. $data [$v]. ' ", ';
}
}
$sql =rtrim ($sql, ', ');//Remove the extra comma from the far right
if (!empty ($condition)) {
$where =isset ($condition [' where '])? $condition [' WHERE ']: ';
$order =isset ($condition [' Order '])? $condition [' Order ']: ';
$limit =isset ($condition [' limit '])? $condition [' Limit ']: ';
if ($where) {//If there is a where condition
$wheretype =gettype ($where);//Determine where the first-level element data type
if ($wheretype = = ' string ') {//If the string is directly connected to the Where condition
$sql. = ' where '. $where;
}else{//If an array, the worth condition of where is traversed
$sql. = ' where 1=1 ';
foreach ($where as $key = = $v) {
$vtype =gettype ($v); Determine where the level two element data type
if ($vtype = = ' array ') {//For arrays, the first value in the array is treated as a connection type (such as:>, <, <=, >=, like ...). ), the second value is treated as a conditional data
if ($v [0]== ' or ') {
$sql. = "". $v [0]. "$key = '". $v [1]. "'";
}else{
$childetype =gettype ($v [1]); Determine the data type of the condition data
if ($childetype = = ' array ') {//For arrays, the first value in the array is treated as a connection type (for example: in ... ), the second value is treated as a conditional data
$sql. = "and $key". $v [0]. " (' ". Implode ($v [1]," ', ' ");
$sql. = "')";
}else{
$sql. = "and $key". $v [0]. " '. $v [1]. "'";
}
}
}else{
$sql. = ' and '. $key. ' = "'. $v. '";//If it is a string type data, the default is the and condition
}
}
}
}
if ($order) {//If there is a sort condition
$ordertype =gettype ($order);//The first-level element data type that determines order
if ($ordertype = = ' string ') {//If the value is a string, direct stitching
$sql. = ' ORDER by '. $order;
}else{//otherwise traversing an array
$sql. = ' ORDER BY ';
foreach ($order as $ke = = $va) {
$sql. = $ke. ' '. $va. ', ';
}
$sql =rtrim ($sql, ', ');//Remove the rightmost comma
}
}
if ($limit) {//If there is a restrictive condition
$limittype =gettype ($limit);//Determine the first-level element data type of limit
if ($limittype = = ' string ') {//If the value is a string, direct stitching
$sql. = ' limit '. $limit;
}else{//Otherwise the second worth of the array is truncated in length
$sql. = ' limit '. $limit [1];

}
}
}
Echo $sql;
}elseif ($type = = ' Delete ') {//delete data
if (empty ($condition)) {
Return "Data deletion must exist data condition";
}
$sql = "Delete from". $dbconfig [' table '];
if (!empty ($condition)) {
$where =isset ($condition [' where '])? $condition [' WHERE ']: ';
$order =isset ($condition [' Order '])? $condition [' Order ']: ';
$limit =isset ($condition [' limit '])? $condition [' Limit ']: ';
if ($where) {//If there is a where condition
$wheretype =gettype ($where);//Determine where the first-level element data type
if ($wheretype = = ' string ') {//If the string is directly connected to the Where condition
$sql. = ' where '. $where;
}else{//If an array, the worth condition of where is traversed
$sql. = ' where 1=1 ';
foreach ($where as $key = = $v) {
$vtype =gettype ($v); Determine where the level two element data type
if ($vtype = = ' array ') {//For arrays, the first value in the array is treated as a connection type (such as:>, <, <=, >=, like ...). ), the second value is treated as a conditional data
if ($v [0]== ' or ') {
$sql. = "". $v [0]. "$key = '". $v [1]. "'";
}else{
$childetype =gettype ($v [1]); Determine the data type of the condition data
if ($childetype = = ' array ') {//For arrays, the first value in the array is treated as a connection type (for example: in ... ), the second value is treated as a conditional data
$sql. = "and $key". $v [0]. " (' ". Implode ($v [1]," ', ' ");
$sql. = "')";
}else{
$sql. = "and $key". $v [0]. " '. $v [1]. "'";
}
}
}else{
$sql. = ' and '. $key. ' = "'. $v. '";//If it is a string type data, the default is the and condition
}
}
}
}
if ($order) {//If there is a sort condition
$ordertype =gettype ($order);//The first-level element data type that determines order
if ($ordertype = = ' string ') {//If the value is a string, direct stitching
$sql. = ' ORDER by '. $order;
}else{//otherwise traversing an array
$sql. = ' ORDER BY ';
foreach ($order as $ke = = $va) {
$sql. = $ke. ' '. $va. ', ';
}
$sql =rtrim ($sql, ', ');//Remove the rightmost comma
}
}
if ($limit) {//If there is a restrictive condition
$limittype =gettype ($limit);//Determine the first-level element data type of limit
if ($limittype = = ' string ') {//If the value is a string, direct stitching
$sql. = ' limit '. $limit;
}else{//Otherwise the first worth start position of the array, the second worth intercept the length
$sql. = ' limit '. $limit [1];

}
}
}
Echo $sql;
}else{//Query data
if (! $field) {
Return "query field cannot be empty";
}
$sql = "Select";
if (!empty ($condition)) {
$distinct =isset ($condition [' distinct '])? $condition [' distinct ']:false;
$group =isset ($condition [' Group '])? $condition [' Group ']: ';
$where =isset ($condition [' where '])? $condition [' WHERE ']: ';
$order =isset ($condition [' Order '])? $condition [' Order ']: ';
$limit =isset ($condition [' limit '])? $condition [' Limit ']: ';
if ($distinct) {//judge whether to go heavy, if true, then go to heavy
$sql. = "distinct";
}
$fieldtype =gettype ($field);//Get field data type
if ($fieldtype = = ' string ') {//If the field type is a string, the field is directly spliced
$sql. = $field;
$field =explode (', ', $field);//Convert the string into an array so that the field below is used
}else{//Otherwise, divide the array into strings and then stitch
$fields =implode (', ', $field);
$sql. = $fields;
}
$sql. = "from". $dbconfig [' table '];//stitching query Datasheet
if ($join) {//If there is an associated data table, iterate through the array, treat the $join key as the table that needs to be associated, $join the first value of the array type as an association, the second value as the primary table's associated field, and the third as the association field of the associated table
foreach ($join as $key = = $v) {
$sql. = "". $v [0]. "Join". $key. "On". $dbconfig [' table ']. ".". $v [1]. " = $key. ". $v [2];
}
}

if ($where) {//If there is a where condition
$wheretype =gettype ($where);//Determine where the first-level element data type
if ($wheretype = = ' string ') {//If the string is directly connected to the Where condition
$sql. = ' where '. $where;
}else{//If an array, the worth condition of where is traversed
$sql. = ' where 1=1 ';
foreach ($where as $key = = $v) {
$vtype =gettype ($v); Determine where the level two element data type
if ($vtype = = ' array ') {//For arrays, the first value in the array is treated as a connection type (such as:>, <, <=, >=, like ...). ), the second value is treated as a conditional data
if ($v [0]== ' or ') {
$sql. = "". $v [0]. "$key = '". $v [1]. "'";
}else{
$childetype =gettype ($v [1]); Determine the data type of the condition data
if ($childetype = = ' array ') {//For arrays, the first value in the array is treated as a connection type (for example: in ... ), the second value is treated as a conditional data
$sql. = "and $key". $v [0]. " (' ". Implode ($v [1]," ', ' ");
$sql. = "')";
}else{
$sql. = "and $key". $v [0]. " '. $v [1]. "'";
}
}
}else{
$sql. = ' and '. $key. ' = "'. $v. '";//If it is a string type data, the default is the and condition
}
}
}
}
if ($group) {//If there is a grouping
$sql. = ' GROUP BY ';
$grouptype =gettype ($group);//Get Group data type
if ($grouptype = = ' string ') {//If the grouping data type is a string, the field is directly spliced
$sql. = $group;
}else{//Otherwise, divide the array into strings and then stitch
$groupstr =implode (', ', $group);
$sql. = $groupstr;
}
}
if ($order) {//If there is a sort condition
$ordertype =gettype ($order);//The first-level element data type that determines order
if ($ordertype = = ' string ') {//If the value is a string, direct stitching
$sql. = ' ORDER by '. $order;
}else{//otherwise traversing an array
$sql. = ' ORDER BY ';
foreach ($order as $ke = = $va) {
$sql. = $ke. ' '. $va. ', ';
}
$sql =rtrim ($sql, ', ');//Remove the rightmost comma
}
}
if ($limit) {//If there is a restrictive condition
$limittype =gettype ($limit);//Determine the first-level element data type of limit
if ($limittype = = ' string ') {//If the value is a string, direct stitching
$sql. = ' limit '. $limit;
}else{//Otherwise the second worth of the array is truncated in length
$sql. = ' limit '. $limit [1];

}
}
}
Echo $sql;
}
[Email Protected]_connect ($dbconfig [' Host '], $dbconfig [' User '], $dbconfig [' Pass '], $dbconfig [' dbname ']);//Pass in Parameters , connect to the database
if (Mysqli_connect_errno ($connect)) {//If the link is faulted, an error message is returned
Return Mysqli_connect_error ($connect);
}
Mysqli_query ($connect, "Set names UTF8");//Set character sets
$recive =mysqli_query ($connect, $sql);//Execute SQL statement and receive return information
if ($type = = ' select ') {
if (Is_object ($recive)) {
$result =array ();
while ($res =mysqli_fetch_assoc ($recive)) {
$result []= $res;
}
return $result;
}else{
Return "Query error, please check the parameter format is correct";
}
}else{
if ($recive ==false) {//If return false means SQL did not execute successfully
Return "Operation error, please check the parameter format is correct";
}else{
Return Mysqli_affected_rows ($connect); Otherwise, the number of rows affected is returned
}
}
Mysqli_close ($connect);
}
Database parameter format
$dbconfig =array (
' Host ' = ' localhost ',
' User ' = ' root ',
' Pass ' = ' root ',
' dbname ' = ' gallery ',
' Table ' = ' gallery '
);
$type = ' select ';//insert type is INSERT, update type is updates, delete is type delete, query type is select
Field parameter format
$field 1= "Gallery_name,mid,image,create_time";
$field 2=array (' Gallery_name ', ' mid ', ' image ', ' create_time ');
When inserting, updating data, the format of the data
$insertdata 1=array (//Insert a
' Gallery_name ' = ' beautiful Changsha ',
' Mid ' =>2,
' Create_time ' =>147258369,
' Image ' = ' uploade/image1.jpg '
);
$insertdata 2=array (//Insert multiple strips
Array
' Gallery_name ' = ' beautiful Changsha ',
' Mid ' =>2,
' Create_time ' =>147258369,
' Image ' = ' uploade/image1.jpg '),
Array
' Gallery_name ' = ' I love my home ',
' Mid ' =>3,
' Create_time ' =>147258369,
' Image ' = ' uploade/image2.jpg '),
Array
' Gallery_name ' = ' Youth ',
' Mid ' =>4,
' Create_time ' =>147258369,
' Image ' = ' uploade/image3.jpg '),
Array
' Gallery_name ' = ' The love of her ',
' Mid ' =>5,
' Create_time ' =>147258369,
' Image ' = ' uploade/image4.jpg ')
);
$where 1= ' gallery_name= ' good world ' or mid=4 and create_time<=147258369 ';
$where 2=array (
' Gallery_name ' and ' The Good world ',
' Mid ' =>array (' or ', 4),
' Create_time ' =>array (' <= ', 147258369),
' Image ' =>array (' like ', '%upload% '),
' ID ' =>array (' in ', Array (2,3,4,5))
);
$order = Array (
' Create_time ' = ' desc ',
' Mid ' = ' ASC '
);
$limit 1 = ' n ';
$limit 2=array (1,3);
Associating data Table parameter formats
$join =array (
' Photos ' =>array (' left ', ' id ', ' gallery_id '),
' Administor ' =>array (' left ', ' mid ', ' ID ')
);
Conditional parameter format
$condition =array (
' WHERE ' = $where 1,
' Order ' = $order,
' Limit ' = $limit 2,
' Distinct ' =>true,
' Group ' =>array (' mid ', ' gallery_name ')
);
$a =phpsql ($dbconfig, $type, $field 1, $insertdata 1, $condition); Calling functions
Var_dump ($a);
?>

Thick PU Education 1718 database connection job answer, split a function that operates a database without writing SQL statements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.