Php Command Line Mode reference http://www.yesky.com/imagesnew/software/php/zh/features.commandline.html
Use the php cli command line to view the PHP installation Module
Php-m
# Apt-Get install php5-gd
# Apt-Get install php5-mysql
#/Etc/init. d/apache2 restart
Test
1. connect to and disconnect the server
<? PHP
$ Db_host = "localhost"; // connection server address
$ Db_user = "root"; // username used to connect to the database
$ Db_psw = "root"; // database connection password
$ Connection = mysql_connect ($ db_host, $ db_user, $ db_psw );
If (! $ Connection ){
Die ('Connection to MySQL Server failed! ');
}
Echo 'Connection to MySQL Server successful! ';
Mysql_close ($ connection );
?>
<! -- By default, after the script is executed, the connection to the server is automatically disconnected, but the mysql_close () function can be used to close the connection at the specified position to release the memory. -->
2. Select a database
<? PHP
$ Db_host = "localhost ";
$ Db_user = "root ";
$ Db_psw = "root ";
$ Db_name = "sunyang ";
$ Connection = mysql_connect ($ db_host, $ db_user, $ db_psw) or die ("connection server failed ");
Mysql_select_db ($ db_name, $ connection) or die ("failed to select database ");
Mysql_close ($ connection );
?>
3. Execute the query
The mysql_query () function returns only one resource identifier for the select, show, explain, and describe statements. If the query execution error occurs, false is returned. For other types of SQL statements, true is returned when mysql_query () is executed successfully, and false is returned when an error occurs.
4. Obtain query results
<? PHP
$ Connection = mysql_connect ("localhost", "root", "root ");
Mysql_select_db ("sunyang", $ connection) or die ("failed to select database ");
$ Query = "select * from employee ";
$ Result = mysql_query ($ query) or die ("query failed"); // execute the query
If (mysql_num_rows ($ result)> 0) {// determines whether the number of rows in the result set is greater than zero.
While ($ ROW = mysql_fetch_array ($ result ))! = False) {// outputs the values in the array through the WHILE LOOP
Echo $ row ['EMP _ id'];
Echo $ row ['emp_number'];
Echo $ row ['emp_name'];
Echo $ row ['emp_age'];
}
} Else {
Echo "no records found ";
}
Mysql_free_result ($ result); // releases the result set memory.
Mysql_close ($ connection );
?>
5. Paging
<? PHP
$ Connection = mysql_connect ("localhost", "root", "root") or die ("failed to connect to the server ");
Mysql_select_db ("sunyang", $ connection) or die ("failed to select database ");
$ Query1 = "select * from employee ";
$ Result = mysql_query ($ query1) or die ("Data Query failed"); // execute the query
$ Nowsperpage = 2; // number of lines displayed on each page
$ Maxrowcount = mysql_num_rows ($ result); // The total number of rows.
If ($ maxrowcount % $ nowsperpage = 0 ){
$ Maxpage = (INT) ($ maxrowcount/$ nowsperpage); // calculate the total number of pages
} Else {
$ Maxpage = (INT) ($ maxrowcount/$ nowsperpage) + 1;
}
If (isset ($ _ Get ['curpage']) {
$ Page = $ _ Get ['curpage']; // obtain the current page number.
} Else {
$ Page = 1;
}
$ Start = $ nowsperpage * ($ page-1); // number of start records
$ Query2 = "select * from employee order by emp_id limit $ start, $ nowsperpage ";
$ Result = mysql_query ($ query2) or die ("Data Query failed"); // execute the query
While ($ ROW = mysql_fetch_array ($ result ))! = False) {// outputs the values in the array through the WHILE LOOP
Echo $ row ['EMP _ id'];
Echo $ row ['emp_number'];
Echo $ row ['emp_name'];
Echo $ row ['emp_age'];
}
If ($ page> 1) {// the current page is not the first page
$ Prevpage = $ page-1; // Previous Page
Echo "<a href = '? Curpage = $ prevpage '> previous page </a> ";
}
If ($ page <$ maxpage ){
$ Nextpage = $ page + 1; // next page
Echo "<a href = '? Curpage = $ nextpage '> next page </a> ";
}
Mysql_free_result ($ result );
Mysql_close ($ connection );
?>
6. Save data
<? PHP
$ Connection = mysql_connect ("localhost", "root", "root ");
Mysql_select_db ("sunyang", $ connection );
$ Query = "insert into employee (emp_number, emp_name, emp_age) values ('sy0804', 'employee4', 27 )";
If (mysql_query ($ query )){
Echo "data saved ";
} Else {
Echo "failed to save data ";
}
Mysql_close ($ connection );
?>
7. modify data
<? PHP
$ Connection = mysql_connect ("localhost", "root", "root") or die ("failed to connect to the server ");
Mysql_select_db ("sunyang", $ connection) or die ("failed to select database ");
$ Query = "Update employee set emp_age = 28 where emp_id = 1 ";
If (mysql_query ($ query )){
Echo "data modified successfully, affected rows:". mysql_affected_rows ();
} Else {
Echo "failed to modify data ";
}
Mysql_close ($ connection );
?>
8. delete data
<? PHP
$ Connection = mysql_connect ("localhost", "root", "root") or die ("failed to connect to the server ");
Mysql_select_db ("sunyang", $ connection );
$ Query = "delete from employee where emp_id = 2 ";
If (mysql_query ($ query )){
Echo "data deleted successfully, affected rows:". mysql_affected_rows ();
} Else {
Echo "failed to delete data ";
}
Mysql_close ($ connection );
?>