To some extent, php is a database of customer end.
Although officials have not recommended, php7.0 even mysql extension has been removed, but to learn about the process-oriented programming ideas is good.
Core step
Connect to the database => execute sql statement
In fact, by extension mysql database operations are built around these two steps of
detailed steps
Detailed flow diagram of the steps of:
Query operation
The following procedure can be achieved queries the local test database class table and output results:
? < PHP header ( "Content-of the type: text / HTML; charset: UTF-8" ); // The first step: connect to the database $ conn = @ mysql_connect ( 'localhost', 'root', 'root' ); IF (! $ Conn ) { Die ( "connection failed, an error message." mysql_error ()); } // step: select database the mysql_select_db ( "Test" ); // third step: character set mysql_set_charset ( "utf8 " ); // step Four: create sql statement $ sql =" the SELECT * from `class`" ; // step five:Execute SQL IF ( $ RES = mysql_query( $ SQL )) { the while ( $ Row = mysql_fetch_assoc ( $ RES )) { echo "<pre>" ; var_dump ( $ Row ); echo "</ pre>" ; } }
// Step Six: Close result set
mysql_free_result ($ RES);
// step Seven: release the connection
mysql_close ($ conn);
() mysql_query function parameters if a query operation, after the implementation of the results of the query returns a pointer to the resource file
If the query fails, it returns an empty string
Mysql_fetch_ the beginning of the function, a total of four, are able to obtain the results of the function:
mysql_fetch_row : return query results in the form of an indexed array
mysql_fetch_assoc : Returns the associative array
mysql_fetch_array : indexed arrays and associative arrays will return
mysql_fectch_object : Returns the object form
To the above code as an example
Mysql_fetch_assoc output results are as follows:
The results output is changed to mysql_fetch_row
dml operations
Query need to obtain a query result, deletions need not change their operation, so slightly different steps:
? < PHP header ( "Content-of the type: text / HTML; charset: UTF-8" ); // The first step: connect to the database $ conn = @ mysql_connect ( 'localhost', 'root', 'root' ); IF (! $ Conn ) { Die ( "connection failed, an error message." mysql_error ()); } // step: select database the mysql_select_db ( "Test" ); // third step: character set mysql_set_charset ( "utf8 " ); // step Four: create sql statement $ sql =" `sname` the Delete from the WHERE class = '谢尔顿李库伯'" ; // step five:Execute SQL $ Exec = mysql_query( $ SQL ); IF ( the mysql_affected_rows ( $ Conn )) { echo 'operation is successful, the recording impact' ; } the else { echo 'operation failed'. Mysql_error (); } // do not need to release resources // sixth step : release the connection mysql_close ( $ conn );
And query operations differences:
1. determine whether the successful implementation should use mysql_affected_rows () function, rather than mysql_query () function:
dml operations are data tables will impact,
mysql_query () in sql statement even if successfully implemented, it may not have an impact on the data table (such as deleting a nonexistent column, or modify the original results and the results of the same)
Therefore to determine whether the operation is successful, it is necessary to determine whether the database had an impact, mysql_affected_rows () can achieve this functionality, database changes, the function will return ture, otherwise false
2. do not need to shut down the result set, because there is no result set itself
3.mysql_query () return value query operations are different from when dml perform, he will return a Boolean value, successful execution is true, failed to false
Modify and delete and insert operations is substantially the same operation, only you need to modify corresponding to sql statement.
Other database operations
In addition to operating in the data table records, mysql database extensions can perform many operations: as show \ drop \ alter, etc.
To delete the data sheet operations as an example: Compared with dml operation, only need to modify the sql statement and determine whether the statements can be executed successfully
? < PHP header ( "Content-of the type: text / HTML; charset: UTF-8" ); // The first step: connect to the database $ conn = @ mysql_connect ( 'localhost', 'root', 'root' ); IF (! $ Conn ) { Die ( "connection failed, an error message." mysql_error ()); } // step: select database the mysql_select_db ( "Test" ); // third step: character set mysql_set_charset ( "utf8 " ); // step Four: create sql statement $ sql =" deletetest drop the Table " ; // step five:Execute SQL IF ( mysql_query ( $ SQL)) { Echo 'successful operation' ; } the else { echo 'operation failed'. Mysql_error (); } // do not need to release resources @ Step Six: releasable connection mysql_close ( $ Conn );