Since it is a database exercise, you first set up a database, let's get one out:
1, enter phpMyAdmin, new database:
2. New Data sheet:
3. New field: (Collation select Utf8_bin)
4, tick a_i, let the ID from the increment:
5. Save:
6. Try inserting data:
7, casually write some information, click to execute:
8, click to browse, look at the effect:
9, insert success, indicating that our table is not a problem, the ID is also self-increasing:
Here is the source code:
<?php/** * Database Exercise * http://www.php.cn/* * mysql_connect-open a connection to the MySQL server * mysql_error-back Text error message generated by previous MySQL operation * * mysql_query-send a mysql query * * mysql_select_db-select mysql database * mysql _get_client_info-get MySQL Client information * mysql_get_host_info-get MySQL host information * mysql_get_proto_info-get MySQL protocol information * mysql_get_server_info-get MySQL server information * mysql_num_rows-get the number of rows in the result set * * mysql_field_name-get the field name of the field specified in the result * mysql_num_fields-Gets the number of fields in the result set * * the Strtoupper () function converts the string to uppercase. * * mysql_close-off MySQL connection *//Step 1: Connect to Database (return resource) $link = mysql_connect ("localhost", "root", ""); Var_dump ($link); Printed Result: Resource (3) of type (MySQL link) echo "<br>"; if (! $link) {echo "Connection database failed <br> error message:"; Echo Mysql_error (); }/** * Step 2: Set operation *///mysql_query ("Set names UTF8"); Set the character set, but it is not recommended, affecting efficiency! Text editor, database use UTF8, there is no need to set character sets/** * step 3: Select a database to use as the default database */mysql_select_db ("IWH"); /** * Step 4: Operation database SQL statement execution * Execution statements are divided into two types: * 1, no result, return TRUE or False * 2, results, successful return result set (Resource), processing resources, extract results from result set and format processing * Statement without result set: DML DCL DDL Create insert Update Delete */Echo Mysql_get_client_info (). " <br> "; Mysqlnd 5.0.11-dev-20120503-$Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $ echo mysql_get_host_info (). " <br> "; localhost via TCP/IP echo mysql_get_proto_info (). " <br> "; Echo Mysql_get_server_info (). " <br> "; 5.5.5-10.1.19-MARIADB//$sql = "CREATE TABLE bro_users (id int)"; CREATE TABLE//$sql = "INSERT into bro_users values (1)"; Insert 1//$sql = "desc users"; Query Table//$sql = "SELECT * from users"; Query Table//$sql = "Show Tables"; Query Table//$sql = "SELECT * from users"; Query Table//$sql = "desc users"; query table Structure $sql = "Select Id,name,nickname,email from Users"; echo "sql:{$sql}<br>"; Http://www.php.cn/: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 view the Insert result//function that can affect the number of rows in Http://www.php.cn/:8888/phpmyadmin/sql.php?db=iwh&table=users (determine if the table has changed) Echo mysql_num_rows (); $result = mysql_query ($sql); Just put a proper SQL to execute Var_dump ($result); If the statement is correct, print: bool (true); If the statement is wrong, print: BOOL (FALSE)//if it is DESC users, print the result set, Print: Resource (4) of type (m Ysql result)//If it is a select * from users, print the results set, Print: Resource (4) of type (MySQL result) echo "< ;br> "; /** * from the resource of the result set, get the result we want, display it in our way or format * mysql_fetch_row-take a row from the result set as an enumeration array * mysql_fetch_assoc-takes a row from the result set as an associative array * mysql_fetch_array-gets a row from the result set as an associative array, or as an array of numbers, or both * mysql_fetch_object-takes a row from the result set as an object * * above command default pointer to first number Each call moves backwards one piece of data, there is data return true, no data returned false */while ($arr = Mysql_fetch_assoc ($result)) {//Print_r ($arr);//echo "<br>"//}//echo "--- ---------------------------<br> ";////print_r (Mysql_fetch_row ($result));//echo" <br> ";////Print_r (Mysql_fetch_assoc ($result));//echo "<br>";////print_r (Mysql_fetch_array ($result));//echo "<br>"; Print_r (Mysql_fetch_object ($result));//echo "<br>"; Echo ' <table border= ' 1 "width=" align= "center" > "; Traverse field echo ' <tr> '; for ($i =0; $i <mysql_num_fields ($result); $i + +) {echo ' <th> ' Mysql_field_name ($result, $i). " </th>> "; } echo ' </tr> '; while ($row = Mysql_fetch_assoc ($result)) {//Traverse table contents echo ' <tr> '; foreach ($row as $col) {echo ' <td> '. $col. ' </td> '; } echo ' </tr> '; } echo ' </table> '; echo "Shared". Mysql_num_rows ($result). " Record!<br> "; echo "Total". Mysql_num_fields ($Result). " Field!<br> "; ----------The following is the recommended wording----------this way more standardized and flexible $result = mysql_query ($sql); Just put a correct SQL can execute echo ' <table border= ' 1 "width=" align= "center" > "; Select Id,name,nickname,email from users If you use this way, then fixed the field name can be directly output, rather than query echo ' <tr> '; Echo ' <th>id</th>> '; Echo ' <th>name</th>> '; Echo ' <th>nickname</th>> '; Echo ' <th>email</th>> '; Echo ' </tr> '; while ($row = Mysql_fetch_assoc ($result)) {//Traverse table contents echo ' <tr> ';//foreach ($row as $col) {// Echo ' <td> ' $col. ' </td> ';/} echo ' <td>{$row [' id ']}</td> ';//Echo ' <TD >{$row [' name ']}</td> '; echo "<td>". Strtoupper ($row [' name ']). " </td> "; echo "<td>{$row [' nickname ']}</td>"; echo "<td>{$row [' email ']}</td> '; Echo ' </tr> '; } echo ' </table> '; echo "Shared". MysQl_num_rows ($result). " Record!<br> "; echo "Shared". Mysql_num_fields ($result). " Field!<br> "; ----------The following is the recommended notation for 2----------$result = mysql_query ($sql); Just put a correct SQL can execute echo ' <table border= ' 1 "width=" align= "center" > "; Select Id,name,nickname,email from users If you use this way, then fixed the field name can be directly output, rather than query echo ' <tr> '; Echo ' <th>id</th>> '; Echo ' <th>name</th>> '; Echo ' <th>nickname</th>> '; Echo ' <th>email</th>> '; Echo ' </tr> '; while (list ($id, $name, $nickname, $email) = Mysql_fetch_row ($result)) {//Traverse table contents echo ' <tr> '; echo "<td>{$id}</td>"; echo "<td>{$name}</td>"; echo "<td>{$nickname}</td>"; echo "<td>{$email}</td>"; } echo ' </table> '; echo "Shared". Mysql_num_rows ($result). " Record!<br> "; echo "Shared". Mysql_num_fields ($result). " Field!<br> "; /** * Step N: Close connection */Mysql_close ();
The above is the Android programmer to learn PHP development (27)-Database practice-phpstorm content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!