php--Package database functions (increase, delete, change) summary of the 2014.5.15

Source: Internet
Author: User

1: Full version of the package function (add (insert), delete (delete), change (update))

Example:<?php

Encapsulating functions for writing database operations

function Mysql_bind () {

First of all, we don't know how many parameters will be passed out.

You can use the Func_get_args () method to get all incoming parameters, and this method returns an array of all parameters

and the Func_get_args () method corresponds to actually have a Func_num_args () to get the number of parameters

$args = Func_get_args ();

By using the Array_shift method, the first parameter passed in, followed by the arguments separated, is actually the SQL statement, and

The values passed in later are separated, and the Array_shift method is used to return a string that is the SQL statement

The rest of the following array is the value to pass in

SQL statements

$sql = Array_shift ($args);

Re-copy to an array

$value = $args;

Replace in the $SQL statement, use the Str_replace function

$sql = str_replace("?", "'%s '", $sql);

Put the replaced $sql statement, through the vsprintf function , fill in the values

$sql = vsprintf ($sql, $value);

intercept $sql the keyword in front of the statement, is actually Select,insert,update,delete

$begin = substr ($sql, 0,6);

      Note: //explode ("", $sql) [0]; //This notation is not supported until PHP5.4

$conn = mysql_connect ("localhost", "root", "") or Die (Mysql_error ());

mysql_select_db ("BBS", $conn);

mysql_query ("Set names ' UTF8 '");

$result = mysql_query ($sql) or Die (Mysql_error ());

if (strcasecmp ($begin, "insert") = = 0) {

If the insert succeeds, then there must be a new ID, so this ID is not equal to 0, the proof of the insertion succeeds, or the failure

return mysql_insert_id ();

} else if (strcasecmp ($begin, "update") = = 0) {

mysql_affected_rows () indicates that a few lines are affected, success is definitely greater than 0, otherwise failed

return Mysql_affected_rows ();

} else if (strcasecmp ($begin, "delete") = = 0) {

mysql_affected_rows () indicates that a few lines are affected, success is definitely greater than 0, otherwise failed

return Mysql_affected_rows ();

} else{

In order to return the contents of the query back

First declare an empty array, and then, this empty array, each loop, assigns $row to him

The equivalent of this array is the formation of a two-dimensional array

$arr = Array ();

while ($row =mysql_fetch_array ($result)) {

$arr [] = $row; //

Array_push ($arr, $row);

}

return $arr;

}

}

?>

2:strcasecmp: Comparison two number who large who small and whether equal (computational method: binary)

Example: strcasecmp ("AB", "AC")

AC ratio AB Large

3:array_shift (): Called the method is to remove the first value (algorithm: FIFO)

4:Array_pop (): The method is called to remove the last one, but returns the last value (algorithm: LIFO)

5:mysql_affected_rows (): indicates that several lines are affected, success, is certainly greater than 0, otherwise failed

6: Regular Expression

Example: var reg = /^[a-za-z][a-za-z0-9]{5,15}$/ ;(Explanation: The username must start with a letter, end with an alphanumeric number, and not exceed 16 digits.

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.