The code I wrote is useless to the DB class library on the Internet. The class library is very powerful, but I have no time to learn it, I just want to write a few small functions I use. This code is reserved as a personal record.
All codes have been debugged by the Sina SAE Platform
There are two field IDs in the database table name test: myname. Here, I will mention that all the SQL statements are directly operated in phpMyAdmin, and then the SQL statement code will appear, which is directly pasted, it can be basically used.
Dbclass. php is attached below
<? Phpclass dbclass {public $ link; // initialize the data connection public function Init () {$ link = mysql_connect (sae_mysql_host_m. ':'. sae_mysql_port, sae_mysql_user, sae_mysql_pass) or die ("error :". mysql_error (); mysql_select_db (sae_mysql_db, $ link); mysql_set_charset ("GBK");} // close the public function CloseTable ($ result) {mysql_free_result ($ result );} // run the SQL statement to obtain the array public function getsql ($ SQL) {// $ SQL =" Select ID, myname from test "; return mysql_query ($ SQL);} // obtain a row of data public function getrow ($ result) {return mysql_fetch_array ($ result, mysql_both );}} /* Self-modified data connection method require_once ("dbclass. PHP "); // first introduce the class library $ db = new dbclass (); $ db-> Init (); $ rows = $ db-> getsql (" select ID, myname from test "); $ ROW = $ db-> getrow ($ rows); echo $ row [" ID "]; $ db-> CloseTable ($ rows ); * // * Baidu's data connection method $ link = mysql_connect (sae_mysql _ Host_m. ':'. sae_mysql_port, sae_mysql_user, sae_mysql_pass); if ($ link) {mysql_select_db (sae_mysql_db, $ link); alias ("GBK"); $ SQL = "select ID, myname from test "; $ result = mysql_query ($ SQL); While ($ ROW = mysql_fetch_array ($ result, mysql_both )) {echo ("<tr> <TD> $ row [0] </TD> <TD> $ row [1] </TD> </tr> ");} mysql_free_result ($ result);} else {echo "database connection Ko";} * // * List Output require_once ("dbclass. PHP "); $ db = new dbclass (); $ db-> Init (); $ rows = $ db-> getsql (" select ID, myname from test "); while ($ ROW = $ db-> getrow ($ rows) {echo ($ row ['id']. "-". $ row [1]. "<br>") ;}$ DB-> CloseTable ($ rows); */?>
Below is the code added for deletion and modification in test. php.
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
I added another inheritance. For the first time, I felt it was so cool to inherit the class library.
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
<? Phpclass dbclass {public $ link; // initialize the data connection public function Init () {$ link = mysql_connect (sae_mysql_host_m. ':'. sae_mysql_port, sae_mysql_user, sae_mysql_pass) or die ("error :". mysql_error (); mysql_select_db (sae_mysql_db, $ link); mysql_set_charset ("GBK");} // close the public function CloseTable ($ result) {mysql_free_result ($ result );} // run the SQL statement to obtain the array public function getsql ($ SQL) {// $ SQL =" Select ID, myname from test "; return mysql_query ($ SQL);} // obtain a row of data public function getrow ($ result) {return mysql_fetch_array ($ result, mysql_both );}} class testclass extends dbclass {// constructor function _ construct () {// parent: printname (); the method of the parent class is called using the parent keyword, same as C #'s base (but it calls the attributes of sub-classes) parent: Init ();} function getall () {return parent: getsql ("select ID, myname from test ");} function insert ($ myn AME) {parent: getsql ("insert into test (ID, myname) values (null ,'". $ myname. "')");} function modify ($ id, $ myname) {parent: getsql ("Update Test Set myname = '". $ myname. "'where id = ". $ id. "Limit 1");} function del ($ id) {parent: getsql ("delete from test where id = ". $ id. "Limit 1") ;}}/* Self-modified data connection method require_once ("dbclass. PHP "); // first introduce the class library $ db = new dbclass (); $ db-> Init (); $ rows = $ db-> getsql (" select ID, my Name from test "); $ ROW = $ db-> getrow ($ rows); echo $ row [" ID "]; $ db-> CloseTable ($ rows ); * // * Baidu's data connection method $ link = mysql_connect (sae_mysql_host_m. ':'. sae_mysql_port, sae_mysql_user, sae_mysql_pass); if ($ link) {mysql_select_db (sae_mysql_db, $ link); alias ("GBK"); $ SQL = "select ID, myname from test "; $ result = mysql_query ($ SQL); While ($ ROW = mysql_fetch_array ($ result, mysql_both )) {Echo ("<tr> <TD> $ row [0] </TD> <TD> $ row [1] </TD> </tr> ");} mysql_free_result ($ result);} else {echo "database connection Ko";} * // * Self-modified output with list require_once ("dbclass. PHP "); $ db = new dbclass (); $ db-> Init (); $ rows = $ db-> getsql (" select ID, myname from test "); while ($ ROW = $ db-> getrow ($ rows) {echo ($ row ['id']. "-". $ row [1]. "<br>") ;}$ DB-> CloseTable ($ rows); */?>
Now, when I use testclass to generate an object, the SQL statement is completely invisible.
-------------------------------
<? PHP?> PHP built-in Environment
Echo () Output Function
The strpos () function is used to find whether a string exists in another string.
$ _ Post ['name'] gets a post variable.
$ _ Get ['id'] gets a get variable.
/// **/# Comment statement
$ Add an YD symbol before the variable name
GetType ($ a_str) to view the type of a Variable
Is_int () is_string () is to check whether it is a variable type.
Settype ($ bar, "string"); force conversion of variable types
= The equal sign is used to determine whether the two sides are equal
Var_dump () Prints information about a variable.
. String Connector
& Reference value assignment example: $ Foo = 'bob'; $ bar = & $ Foo; in this way, the address reference value is assigned.
Global global variable definer PHP default internal variables and external variables are out of order unless you redefine them.
Static $ A = 0; static variable values are not defined after the first definition.
$ Variable is similar to reference example $ A = 'hello'; $ A = 'work'; // equivalent to $ Hello = 'work ';
$ {$ A [1]} variable variables must be included to ensure accuracy when an array is encountered.
$ _ Server ['php _ Self ']; address of its own page
$ _ Cookie ['Count'] Read cookie
Setcookie ('Count', $ count, time () + 3600); write cookie
Const constant = 'Hello world'; defines Constants
_ Line _ file _ DIR _ function _ class _ method _ namespace _ magic constant
. = Used for String concatenation
See php5.3 reference manual Language Reference-> class and Object