Home >Backend Development >PHP Tutorial >PHP concise function (PHP simple and clear function syntax)_PHP tutorial

PHP concise function (PHP simple and clear function syntax)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:18:15749browse

1. Related to mysql

mysql_connect
Establish a connection with the MySQL server
Syntax


resource mysql_connect(string server[,string usingname[,string password[, bool new_link[,int client_flags]]]])
eg:

Copy code The code is as follows:

$DB_HOST ="localhost";
$DB_LOGIN ="root";
$DB_PASSWORD =" 123456";
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);


mysql_data_seek
Move the internal query pointer to the query row
Syntax

bool mysql_data_seek(resource result_indetifier,int row_number)
eg:
Copy code The code is as follows:

$DB_HOST ="localhost";
$DB_LOGIN ="root";
$DB_PASSWORD = "123456";
$DB_NAME ="flag";
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);
$res=mysql_query(" SELECT * FROM PRODUCT");
$row=mysql_fetch_array($res);
for($i=0;$i<$num;$i++)
$row=mysql_fetch_array($res);
mysql_data_seek($res,0);//Move the pointer back to the first row of the query result




mysql_fetch_array
Save the query result In the array (each array element stores a record)
Syntax

array mysql_fetch_array(resource result[,int result_type])
eg
Copy code The code is as follows:

$DB_HOST ="localhost";
$DB_LOGIN ="root";
$DB_PASSWORD ="123456";
$DB_NAME ="flag";
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);
$res=mysql_query("SELECT * FROM PRODUCT");
$row=mysql_fetch_array($res);



mysql_fetch_object
Get a row of query results and store it as an object type, which is exactly the same as MySQL_fetch_array(). The difference is that mysql_fetch_object() can only obtain query results through field names
echo $row->fieldname; //Correct usage
echo $row->0; //Incorrect usage
Syntax

object mysql_fetch_object(resource result)
eg
Copy code The code is as follows:

$DB_HOST = "localhost";
$DB_LOGIN ="root";
$DB_PASSWORD ="123456";
$DB_NAME ="flag";
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD );
mysql_select_db($DB_NAME);
$res=mysql_query("SELECT * FROM PRODUCT");
$row=$mysql_fetch_object($res);
while($row)
{
echo $rowàp_id;
echo $rowàp_name;
}



mysql_insert_id
After using the INSERT command to add a piece of information, you can Use this function to obtain the unique id of the record just added
Syntax

int mysql_insert_id([esource link_identifier])
eg
Copy code The code is as follows:

$DB_HOST ="localhost";
$DB_LOGIN ="root";
$DB_PASSWORD ="123456";
$DB_NAME ="flag" ;
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);
$SQLStr"INSERT INTO produce (p_id,p_name)VALUES('','PHP book ')";
$res=mysql_query($res);
$p_id=mysql_insert_id();


mysql_num_rows
Get the number of rows in the query result
Syntax

int mysql_num_rows(resource result)
eg
Copy code The code is as follows:

$DB_HOST ="localhost";
$DB_LOGIN ="root";
$DB_PASSWORD ="123456";
$DB_NAME ="flag";
$conn=mysql_connect($DB_HOST,$ DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);
$res=mysql_query("SELECT * FROM PRODUCT");
$num=mysql_num_rows($res);


mysql_query
Send a SQL syntax query statement
Syntax

resource mysql_query(string query[,resource link_identifier])
eg
Copy code The code is as follows:

$DB_HOST ="localhost";
$DB_LOGIN ="root";
$DB_PASSWORD="123456";
$DB_NAME ="flag";
$conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);
$res=mysql_query("SELECT * FROM PRODUCT ");



mysql_select_db
Select the name of the database to be accessed
Syntax

bool mysql_select_db(string database_name[,resource link_identifier])
eg
Copy code The code is as follows:

$DB_HOST ="localhost";
$DB_LOGIN ="root";
$DB_PASSWORD ="123456";
$DB_NAME ="flag";
$conn=mysql_connect ($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
mysql_select_db($DB_NAME);

2. File system function
copy
Copy text
Syntax
bool copy(string source,string dest)
eg
Copy code The code is as follows:

copy("abc. txt","/tmp/newabc.txt");

fclose
Close an open file pointer
Syntax
bool fclose(resource handle)
eg
Copy code The code is as follows:

$fp=fopen("abc.txt","w");
fclose($fp);

fgets
Get the contents of the column from the position pointed to by the file pointer
Syntax
string fgets(resource handle[,int length])
eg
Copy code The code is as follows:

$fp=fopen("abc.txt","w");
$txtdata= fgets($fp,4096);

file
Read the entire file content into an array
Syntax
array file(string filename[,int use_include_path [,resource context]])
eg
Copy code The code is as follows:

$content=file("abc .txt");


file_exists
Check if the file exists
Syntax

bool file_exists(string filename)
eg
Copy code The code is as follows:

if (file_exists("abc.txt"))
echo "This file exists";
else
echo "This file does not exist";

filesize
Get file size
Syntax
int filesize(string filename)
eg
Copy code The code is as follows:

$size=filesize("abc.txt");

fopen
Open a file or url
Syntax
resource fopen (string filename,string mode[,bool use-include_path[,resource zcontext]])
eg
Copy code The code is as follows:

$fp=fopen("abc.txt");
$fp=fopen("http://www.jb51 .net/bacteroid/","r");


fputs
Write data to the file
Syntax
int fputs(resource handle,string string[ ,int length])
eg
Copy code The code is as follows:

$fp=fopen("abc.txt ");
fputs($fp,"helloworld!");


fseek
Set the position pointed by the file pointer
Syntax
int fseek( resource handle,int offset[,int whence])
eg
Copy code The code is as follows:

$fp= fopen("abc.txt","w");
$txtdata=fgets($fp,4096);
fseek($fp,0);//Point the pointer back to the beginning


mkdir
Create a directory
Syntax
bool mkdir(string pathname[,int mode[,bool recursive[,resource context]]])
eg
Copy code The code is as follows:

mkdir("ljt/newfolder");


unlink
Delete file
Syntax
int unlink(string filename);
eg
Copy code The code is as follows:

unlink("abc.txt");

3. Date and time functions
data
Return the local time/date in the specified format
Syntax
string date(string format[,int timestamp])
eg
Copy code The code is as follows:

$time =date("Y-m-d g:i:s");


getdate
Get date and time information
Syntax
array getdata([int timestamp ])
eg
Copy code The code is as follows:

$now=getdate();
$year =$now["year"];
$month=$now["month"];


gettimeofday
Get the current time (including GMT)
Syntax
array gettimeofday(void)
eg
Copy code The code is as follows:

$time=gettimeofday();

4. String processing function
explode
according to the specified separation Split the string into an array
Syntax
array explode(string separator,string string[,int limit])
eg
Copy code The code is as follows:

$str="a,b,c";
$res=explode(",",$str);//$res[0] =a


implode
Concatenate the array contents into a string
Syntax
string implode(string glue,array pieces)
eg
Copy code The code is as follows:

$newarray=array('a','b','c');
$ res=implode(",",$newarray);//$res=a,b,c

strlen
Get the length of the string
Syntax
int strlen( string string)
eg
Copy code The code is as follows:

strlen("www.jb51.net"); //Return 15
[c/ode]
substr
Get a certain part of the characters (substring) specified by the string
Syntax
string substr("www.gxnu.edu. cn",1,7); //Return "ww.gxnu"
5. Mathematical function library
Unconditionally carry the decimal part of the floating point number
Syntax
float ceil(float value)
eg
[code]
echo ceil(9.99);//Return 10
echo ceil(9.12);//Return 10

cos
Get floating point number Cosine of value
Syntax
float cos(float arg)
eg
Copy code The code is as follows:

$numcos=cos(0.5);

floor
Unconditionally remove the decimal part of the floating point number
Syntax
float floor(floor value)
eg
Copy code The code is as follows:

echo floor(9.12);//Return 9
echo floor(9.99); // Return 9

rand
Generate a range of random values ​​
Syntax
i
nt rand([int min,in max])
eg
Copy code The code is as follows:

$num=rand(0,100);//Generate a number between 1 and 100 Random value

round
Round the decimal part of the floating point number
Syntax
float round(float value)
eg
Copy code The code is as follows:

float round(9.99)//returns 10
float round(9.12)//returns 9

sin
Get the sine value of a floating point value
Syntax
float sin(float arg)
eg
Copy code The code is as follows:

$numsin=sin(0.5);


6. Session function
session_register
Describes one or more Sessions The variable
syntax
bool session_register(mixed name[,mixed...])
eg
Copy code The code is as follows:

$name="flag";
session_register("name");


session_start
Initialize Session information
Syntax
bool session(void)
eg
Copy code The code is as follows:

session_start();


7. Array function
count
Calculate how many array functions there are in the array
Syntax
int count(mixed var[,int mode])
eg
Copy code The code is as follows:

count($array);

list
Assign the element value in the array to the variable
Syntax
void list(mixed varname,mixed...)
eg
Copy code The code is as follows:

$array=array(a,b,c);
list($str1,$str2,$str3)=$array;//$str1=a


range
Create an array within the specified range
Syntax
array range(int low,int high[,int step])
eg
Copy code The code is as follows:

$array=array(0,9);

shuffle
shuffle the array The elements in are randomly reordered
Syntax
bool shuffle(array array)
eg
Copy code The code is as follows:

shuffle($array);


Author: Bacteroides

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325584.htmlTechArticle1. Mysql-related mysql_connect establishes a connection with the MySQL server syntax resource mysql_connect(string server[,string usingname[ ,string password[, bool new_link[,int client_f...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Let PHP provide file download code faster_PHP tutorialNext article:Let PHP provide file download code faster_PHP tutorial

Related articles

See more