Since it is a database exercise, create a database first. let's come up with one: 1. go to phpmyadmin and create a database. since it is a database exercise, create a database first, let's come up with the following:
1. go to phpmyadmin and create a database:
The following is the source code:
"; If (! $ Link) {echo "failed to connect to the database
Error message: "; echo mysql_error ();}/*** step 2: set operation * // mysql_query (" set names utf8 "); // set the character set, but it is not recommended to use it, which affects efficiency! // Utf8 is used for text editors and databases. you do not need to set the character set here/*** step 3: Select a database as the default database and use */mysql_select_db ("iwh "); /*** step 4: execute SQL statements for database operations * There are two types of execution statements: * 1. if no result is displayed, true or false is returned * 2. if there is a result, if the execution succeeds, return the result set (resource), process the resource, retrieve the result from the result set, and format the statement for processing * No result set: dml dcl ddl create insert update delete */echo mysql_get_client_info ()."
"; // Mysqlnd 5.0.11-dev-20120503-$ Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $ echo mysql_get_host_info ()."
"; // Localhost via TCP/IP echo mysql_get_proto_info ()."
"; // 10 echo mysql_get_server_info ()."
"; // 5.5.5-10.1.19-MariaDB // $ SQL =" create table bro_users (id int )"; // create a table // $ SQL = "insert into bro_users values (1)"; // insert 1 // $ SQL = "desc users "; // query the table // $ SQL = "select * from users"; // query the table // $ SQL = "show tables "; // query the table // $ SQL = "select * from users"; // query the table // $ SQL = "desc users"; // query the table structure $ SQL = "select id, name, nickname, email from users "; echo" SQL: {$ SQL}
"; // #: 8888/myPhpDemo/mysqlDemo. php? Name = liudehua & password = 123456 & nickname = huazai & email = hua@qq.com // $ SQL = "insert into users (name, password, nickname, email) values ('{$ _ GET ['name']}', '{$ _ GET ['password']}', // '{$ _ GET ['nickname']}', '{$ _ GET ['email']} ') "; // bool (true) // you can go to #: 8888/phpmyadmin/SQL. php? Db = iwh & table = users check the insert result // function that can affect the number of rows (to determine whether the table has changed) // echo mysql_num_rows (); $ result = mysql_query ($ SQL ); // run var_dump ($ result) as long as a correct SQL statement is put; // if the statement is correct, Print: bool (true); if the statement is incorrect, Print: bool (false) // if it is desc users, print the result set, Print: resource (4) of type (mysql result) // if it is select * from users, print the result set, print: resource (4) of type (mysql result) echo"
";/*** Get the expected result from the result set resource, display * mysql_fetch_row in our method or format-retrieve a row from the result set as an enumeration array * mysql_fetch_assoc-retrieve a row from the result set as an associated array * mysql_fetch_array-retrieve a row from the result set as an associated array, or number array, or both * mysql_fetch_object-get a row from the result set as an object ** the above command points to the first data by default, each call moves a piece of data backward, and returns true if there is data, if no data is returned, false * // while ($ arr = mysql_fetch_assoc ($ result) {// print_r ($ arr); // echo"
"; //} // Echo "------------------------------
"; // Print_r (mysql_fetch_row ($ result); // echo"
"; /// Print_r (mysql_fetch_assoc ($ result); // echo"
"; // Print_r (mysql_fetch_array ($ result); // echo"
"; // Print_r (mysql_fetch_object ($ result); // echo"
"; Echo'
'. Mysql_field_name ($ result, $ I )."> ";} Echo'
'; // Print the echo field'
'; For ($ I = 0; $ I
'; While ($ row = mysql_fetch_assoc ($ result) {// traverse the table content echo'
'; Foreach ($ row as $ col) {echo'
'. $ Col .' | ';} Echo'
';} Echo'
'; Echo "total". mysql_num_rows ($ result). "record!
"; Echo" a total of ". mysql_num_fields ($ result)." fields!
"; // ---------- The following is the recommended writing method ---------- which is more standardized and flexible $ result = mysql_query ($ SQL); // you can execute echo if you put a correct SQL statement'
'; // Select id, name, nickname, email from users if you use this method, you can directly output the field name without querying echo'
'; Echo'
Id | > '; Echo'
Name | > '; Echo'
Nickname | > '; Echo'
Email | > '; Echo'
'; While ($ row = mysql_fetch_assoc ($ result) {// traverse the table content echo'
'; // Foreach ($ row as $ col) {// echo'
'. $ Col .' | '; //} Echo"
{$ Row ['id']} | "; // Echo"
{$ Row ['name']} | "; Echo"
". Strtoupper ($ row ['name'])." | "; Echo"
{$ Row ['nickname']} | "; Echo"
{$ Row ['email ']} | "; Echo'
';} Echo'
'; Echo "total". mysql_num_rows ($ result). "record!
"; Echo" a total of ". mysql_num_fields ($ result)." fields!
"; // ---------- The following is the recommended syntax: 2 ---------- $ result = mysql_query ($ SQL); // you can execute echo if you put a correct SQL statement'
'; // Select id, name, nickname, email from users if you use this method, you can directly output the field name without querying echo'
'; Echo'
Id | > '; Echo'
Name | > '; Echo'
Nickname | > '; Echo'
Email | > '; Echo'
'; While (list ($ id, $ name, $ nickname, $ email) = mysql_fetch_row ($ result) {// traverse table content echo'
'; Echo"
{$ Id} | "; Echo"
{$ Name} | "; Echo"
{$ Nickname} | "; Echo"
{$ Email} | ";} Echo'
'; Echo "total". mysql_num_rows ($ result). "record!
"; Echo" a total of ". mysql_num_fields ($ result)." fields!
";/*** Step N: close the connection */mysql_close ();
The above is the content of PHP Development (27)-database exercises-PhpStorm for Android programmers. For more information, see PHP Chinese website (www.php1.cn )!