One: Web development is divided into
1. Static Web development (HTML page)
2: Dynamic Web development.
Post, pay online, send text messages, send emails.
PHP is embedded in the HTML code, and the PHP script runs on the server side.
Second: Database programming
1.Mysql
2.Mysqli
Pdo
1.mysql environment construction.
in PHP. INI to configure the MySQL extension library
Extension=php_mysql.dll.
You can use the Phpinfo () function
When you create a password in the database, use MD5 to encrypt it.
Chinese cannot add an issue.
Use ANSI can add, use UTF8 cannot, client recognize GBK code.
Use showvariableslike ' Xcharx '; Displays the display type of all object names
And then reset it again.
Query again.
Three: Write the PHP program, complete the display of the user table.
Step: (MySQL data go)
1. Get the connection.
2. Select the database.
3. Set the operation code.
4. Send instructions to the database. SQL (DDL data definition statement, DML data manipulation language, DQL data query Language, DTL data thing statement)
5. Accept the results of the return and deal with them.
Release the resource and close the connection.
Create a database test;
Showdatabases Show All the databases
usetest; Specifies which database to use.
Showtables; Displays the table for the specified database.
Create a user table
CREATE TABLE User2 (
ID int primary KEY auto_increment,
Name varchar (+) is not NULL,
Password varchar (+) NOT NULL,
Email varchar (+) NOT NULL,
Age tinyint unsigned NOT NULL
)
Droptableuser1; Delete a table
Adding data to a table
Insertintouser (Name,password,email,age) VALUES (' Aquarius ', MD5 (' 123456 '), ' 3649872 ', ' 18 ');
View the data select*fromuser1 in the table;
Use this statement to query a variable type
Var_dump ($res);
The Web page will output the following results.
One of the simplest PHP programs to complete the operation of the database.
<span style= "FONT-SIZE:14PX;" ><?php$conn=mysql_connect ("127.0.0.1", "root", "421566"), if (! $conn) {die ("Connection Failed". Mysql_error ());} mysql_select_db ("Test"), $sql = "Select*fromuser", $res =mysql_query ($sql, $conn);//Use this statement to query the variable type//var_dump ($res); /will take the next row of data from the result set while ($row =mysql_fetch_row ($res)) {///The first extraction, with ¥row[i]//echo "<br/> $row [0]--$row [1]--$row [2]" ;//echo "<br/>";//var_dump ($row);//The Second way foreach ($rowas $key=> $var) {echo "--$var";} echo "<br/>";} After using the results, be sure to release Resources mysql_free_result ($res);//This sentence if not, the system will automatically shut down. Mysql_close ($conn);? ></span>
Results.
If the DQL statement is executed, the result of the query returned
If you are executing a DML statement, you are returning a bool
The result of extracting the query from Mysqlresult is as follows
mysql_fetch_row//returns an index data
mysql_fetch_assoc//returns an associative array
The difference is in writing the name of the field.
mysql_fetch_array//returns an indexed array and an associative array
mysql_fetch_object//to return the one-flight data as an object
Four: The table user to increase, delete, change operation. (DML operation)
<?php$conn=mysql_connect ("127.0.0.1", "root", "421566"), if (! $conn) {die ("Connection Failed". Mysql_error ());} mysql_select_db ("Test", $conn); mysql_query ("Setnamesutf8");//$sql = "Insertintouser (name,password,email,age) VALUES (' King Shu ', MD5 (' 123 '), ' [email protected] ', ' + '), '//$sql = ' deletefromuserwhereid=1 '; $sql = "updateusersetage= 100whereid=2 "; $res =mysql_query ($sql, $conn), if (! $res) {die (" operation failed ". Mysql_error ());} View by a few data if (Mysql_affected_rows ($conn) >0) {echo "Operation succeeded";} Else{echo "Number of rows not affected";}? >
As can be seen from the above file, the reusability and maintainability of code Oh, well, in PHP programming, it's usually the operation of the database, encapsulated as a tool class SqlHelper.
<?php classsqltool{Private$conn; private$host= "localhost"; Private$user= "Root"; Private$password= "421566"; private$db= "Test"; Functionsqltool () {$this->conn=mysql_connect ($this->host, $this->user, $this->password); if (! $this->conn) {die ("connectfailed". Mysql_error); } mysql_select_db ($this->db, $this->conn); The following statement will be used in the Web page in Chinese garbled//mysql_query ("Setnamesutf8"); } publicfunctionexecute_dql ($sql) {$res =mysql_query ($sql) Ordie ((mysql_error)); Return$res; } publicfunctionexecute_dml ($sql) {$b =mysql_query ($sql, $this->conn); if (! $b) {return0; } else{if (Mysql_affected_rows ($this->conn) >0) {Return1;//echo "Operation succeeded"; } else{Return2;//echo "Number of rows not affected"; }}}}?>
<?php/* $conn =mysql_connect ("127.0.0.1", "root", "421566"), if (! $conn) {die ("Connection Failed". Mysql_error ());} mysql_select_db ("Test", $conn); mysql_query ("Setnamesutf8");//$sql = "Insertintouser (name,password,email,age) VALUES (' King Shu ', MD5 (' 123 '), ' [email protected] ', ' + '), '//$sql = ' deletefromuserwhereid=1 '; $sql = "updateusersetage= 100whereid=2 "; $res =mysql_query ($sql, $conn), if (! $res) {die (" operation failed ". Mysql_error ());} View by a few data if (Mysql_affected_rows ($conn) >0) {echo "Operation succeeded";} Else{echo "Number of rows not affected";} */require_once "SqlTool.class.php";//$sql = "Insertintouser (name,password,email,age) VALUES (' Chengwen ', MD5 (' 123 '), "[Email protected] '," "; $sql =" select*fromuser ";//Create an Object $sqltool=newsqltool ();//$sqlTool->execute_dml ($sql) ; $res = $sqlTool->execute_dql ($sql), while ($row =mysql_fetch_object ($res)) {foreach ($rowas $key=> $var) {echo "-- $var ";} echo "<br/>";} Mysql_free_result ($res);? >
The reason for the error is that static methods cannot manipulate dynamic variables.
Five: Accept a table name, and then display the table in a tabular form on the Web page.
<?php Functionshow_tab_info ($table _name) {$conn =mysql_connect ("localhost", "root", "421566"); mysql_select_db ("Test", $conn); $sql = "Select*from$table_name"; $res =mysql_query ($sql, $conn);//To know the total number of rows and how many columns $rows =mysql_affected_rows ($conn); $colums =mysql_num_fields ($res); echo "$rows = $colums"; echo "<tableborder=1><tr>"; for ($i =0; $i < $colums; $i + +) {$field _name=mysql_field_name ($res, $i); echo "<th> $field _name</th>"; } echo "</tr>"; while ($row =mysql_fetch_row ($res)) {echo "<tr>"; for ($i =0; $i < $colums; $i + +) {echo "<td> $row [$i]</td>"; }} echo "</table>"; while ($field _info=mysql_fetch_field ($res)) {//echo "<br/>". $field _info->name; }//var_dump ($filed _info); } show_tab_info ("User");
Php+mysql Getting Started notes