PHP reference commonly used procedures (UNIX operating system)
PHP reference commonly used procedures (UNIX operating system) 2004-06-24 click: 68 common procedures PHP reference (UNIX operating system)
Common Reference (UNIX operating system) 1, Database Connection documents: conn.php (connecting MYSQL database configuration file) <?
/ / This document is connected to the database configuration file, in this connection parameters defined
$ Host = "localhost";
$ User ="******";// you need modify here, replace your database's account
$ Passwd ="******";// also modify here, replace your database's password
$ Dbname ="******";// and modify here, replace your database's name if (! $ Link = mysql_connect ( "$ host", "$ user", "$ pwd")) / / start connect your database
(
Print 'Could not connect to database';
Exit;
)
Mysql_select_db ( "$ dbname") or die ( "Could not select database");
–> 2, the database table files: list_alltb.php (list all the tables in the database name)
<?
/ / Demonstration of how a list of all the database tables
Include ( "conn.php");
$ Result = mysql_list_tables ($ dbname);
If (! $ Result) (
Print "DB Error, could not list tables \ n";
Print 'MySQL Error:'. Mysql_error ();
Exit;
)
$ I = 0;
Echo "$ dbname database in the table is as follows: <br >";// the code below start tolist all the tables in the database";
Echo "<table border=1>";
While ($ i <mysql_num_rows ($ result)) (
$ Tb_names [$ i] = mysql_tablename ($ result, $ i);
Echo "<tr> <td> $ tb_names [$ i] </ td> </ tr> \ n";
$ I + +;
)
Echo "</ table>";
Mysql_free_result ($ result); / / free the resource at the end
–>
3, database query documents: selectdb.php (database query, display of results) <? Php
/ / Demonstrate how to query the database
Include ( "conn.php");
/ * * The implementation of SQL query /
$ Query = "SELECT * FROM my_table";
$ Result = mysql_query ($ query) or die ( "Query failed");
/ * In HTML print the results * /
Print "<table> \ n";
While ($ line = mysql_fetch_array ($ result, MYSQL_ASSOC)) (
Print "\ t <tr> \ n";
Foreach ($ line as $ col_value) (
Print "\ t \ t <td> $ col_value </ td> \ n";
)
Print "\ t </ tr> \ n";
)
Print "</ table> \ n";
/ * Release resources * /
Mysql_free_result ($ result);
/ * Disconnect * /
Mysql_close ($ link);
–>
4, database manipulation of documents: operatedb.php (database records to add, delete, modify) <?
/ / Demonstration of how the data in the database to insert, update and delete operations
Include ( "conn.php");
$ Sql = "insert into user (ID, PW, Name, Sex, Email, Title, Info) values ( '$ userid', '$ userpw', '$ usernam
E ',' $ usersex ',' $ usermail ',' $ usertitle ',' phrase inserted $ userinfo')";//
Mysql_query ($ sql) or die (mysql_error ());// implementation insert operation
$ Sql = "delete from user where ID = '$ userid'"; / / delete statements
Mysql_query ($ sql) or die (mysql_error ()) / / delete the implementation of Operation
$ Sql = "update user set PW = '$ userpw' Name = '$ username' Sex = '$ usersex' Email = '$ usermail'
Title = '$ usertitle', Type = '$ usertype' Info = '$ userinfo' where ID = '$ userid' "; / / update statements
Mysql_query ($ sql) or die (mysql_error ()) / / delete the implementation of Operation
Mysql_close ($ link); / / disconnect
–>
5, document procedures: fileoperate.php (the most common file operations) <?
$ Filename ="****";// to the operation of the file name
/ / Read operation, read out a document to all the contents of a string variable
$ Content = file ($ filename);
$ Content = join ("",$ content);
Print $ content;
/ / A string of characters to write the contents of the original document
If (! $ Fp = fopen ($ filename, "w "))//" w" method to open the file, if the file does not exist, create the document if it exists, the original documents are covered
(
Die ( "open file $ filename error!");
)
Fputs ($ fp, $ content, strlen ($ content));
Fclose ($ fp); / / finished after closing documents must be promptly handle
/ / Added to the document at the end of
If (! $ Fp = fopen ($ filename, "a "))// added to the document at the end," a "way to open
(
Die ( "open file $ filename error!");
)
Fputs ($ fp, $ content, $ strlen ($ content));
Fclose ($ fp);
/ / Delete files
If (is_file ($ filename))
(
Unlink ($ filename) or die ( "delete files failure");
)
Tags: Java procedures, java system, java unix, System






