PHP common program reference (UNIX operating system) 2004-06-24 Click: 68
PHP common program reference (UNIX operating system)
Common programming (UNIX operating system)
1. Database connection file: Conn.php (Configuration file connection to the MySQL database) // This file is the configuration file for the database connection, the connection parameters are defined here $ 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 (! $ limited "," $ us), "$ pww") // start connect your database {print 'Could Not connect to Database'; exit } mysql_select_db ("$ dbname") or Die ("Could Not Select Database");?> 2, Database list file: list_alltb.php (list all the table names in the database) // Demo how to list one All tables of the database 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 "Database $ dbname has the following: "; // the code below start tolist all the tables in the database "; echo"
$ TB_NAMES [$ I] td> TR > / n "; $ I ;} echo" table> "; mysql_free_result ($ result); // free the res Ource at the end?> 3, database query file: selectdb.php (Database query, display) php // Demo how to query database include ("Conn.php"); / * Execute SQL Query * / $ Query = "SELECT * from my_table"; $ results = mysql_query ($ query) or Die ("query failed"); / * Print results in HTML * / print "
/ n"; while ($ line = mysql_fetch_Array ($ result, mysql_assoc)) {Print "/ t
/ n"; foreach ($ line as $ col_value) {print "/ t / t <
TD> $ col_value td> / n ";} print" / t Tr> / n ";} print" table> / n "; / * Release Resource * / MySQL_Free_Result ($ results); / * Disconnect * / mysql_close ($ LINK);?> 4, database manipulation file: operatedb.php (Increase, delete, modification) // Demonstrate how to insert, delete and update data in the database Operation include ("Conn.php"); $ SQL = "INSERT INTO USER (ID, PW, Name, SEX, Email, Title, Info) Values ('$ Userid', '$ Userpw', '$ Username",' $ UserSex ',' $ Usermail ',' $ UserTitle ',' $ UserInfo ') "; // Insert Statement Mysql_Query ($ SQL) OR Die (MySQL_ERROR ()); // Perform Insert Operation $ SQL =" Delete from User WHERE ID = '$ userid' "; // Delete Statement Mysql_Query ($ SQL) or Die (MySQL_ERROR ()); // Execute Delete Action $ SQL =" Update User Set Pw = '$ Userpw', Name = '$ Username ', Sex =' $ usermail ', Title =' $ @ertle ', Type =' $ userTYPE ', INFO =' $ userInfo 'where id =' $ userid '"; // Update statement mysql_Query ($ SQL) or Die (MySQL_ERROR ()); // Performs Delete Action MySQL_Close ($ LINK); // Disconnect?> 5, File Operator: FileOperate.php (most common file operation) $ filename = "****"; // To operate file name // read operation, read a file all content to a string variable $ content = file ($ filename); $ content = join ("", $ Content); Print $ Content; // Remove the content of the character string to the original file IF (! $ fp = fopen ($ filename, "w")) // "W" opens the file, if Create a file does not exist This file; if present, override the original file {Die ("Open file $ filename ERROR!");} Fputs ($ FP, $ Content, Strlen ($ Content); fclose ($ fp); // After writing To close the file handle // append to the end of the file IF (! $ Fp = fopen ($ filename, "a"))))) // append to the end of the file, open {Die in "a" mode ("Open file $ filename Error! ");} fputs ($ FP, $ Content, $ Strlen ($ content); fclose ($ fp); // Remove file IF (is_file ($ filename) {unlink ($ filename) or Die (" Delete file failure");