Database connection steps
We have organized the database connection into the 8 most important steps for everyone, jokingly calling it: "The eight steps of database connection. ".
The eight steps are as follows, and the functions used in each step are explained:
Step one: Connect to the database server
If parameter 4 and database name have been filled in and selected in this step, there is no need to perform the third step.
Step Two: Judgment Error
Step 3: Select the database
If the database has been filled in in the first step and does not need to be changed to another database, then no Perform step three.
Step 4: Set character set
Step 5: Prepare SQL statement
is actually a string of SQL statements.
For example:
$sql = "insert into user(username,password) values('$username','$password')";
We usually want to Variable assignment is used in SQL statements. However, there is an error in the variable or SQL statement, which is very difficult to troubleshoot.
We have added this step based on actual work experience.
If an error is reported when executing this step, we can print out the SQL statement and paste it into phpMyAdmin or related tools.
When debugging, if the execution is successful, it means that the problem is not with the SQL statement. If execution fails, double check the SQL statement.
Step 6: Send the SQL statement
The SQL statement is prepared and needs to be sent to the SQL statement through mysqli_query The statement is sent to the MySQL server.
The MySQL server will execute the SQL statement sent.
Step 7: Determine whether the execution is normal or traverse the data
Read
In step 6, a statement of the select category is sent, and the result output usually needs to be displayed. You need to use the function that traverses the display data.
##Write
In step 6, if the insert statement is sent, you usually need to get whether the execution is successful, or get the self-incremented ID at the same time.Modification and deletion
In step 6, if the statements of update and delete categories are sent. Just need to determine whether the execution is successful. We list the data tables of these commonly used functions for everyone to check.Step 8: Close the database
The database connection is a resource type. We told you about it when we explained resource types in the previous chapter. All resource types involved are either opened or closed. This ensures that PHP processes and recycles resources more efficiently. Therefore, after the database connection is successful, there is no need to use it. We can close this connection. Others: Display server information functionNote: Mysqli only needs to learn the procedural method. In the actual work of the object-oriented stage, the object usage of mysqli was completely abandoned, and instead the PDO object was used to connect to the database.
1. In order to better set up the data connection, the values involved in the data connection are generally defined as variables.
<?php $mysql_server_name='localhost'; //改成自己的mysql数据库服务器 $mysql_username='root'; //改成自己的mysql数据库用户名 $mysql_password='123456789'; //改成自己的mysql数据库密码 $mysql_database='php'; //改成自己的mysql数据库名 ?>You can also put the above variables in a file can be called by other files at any time.For example: put the above content in: db_config.php and then call it directly on other pages that need to use the database.Calling code: require( "db_config.php");
2. Connect to the database
<?php $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting") ; //连接数据库 mysql_query("set names 'utf8'"); //数据库输出编码 应该与你的数据库编码保持一致.南昌网站建设公司百恒网络PHP工程师建议用UTF-8 国际标准编码. mysql_select_db($mysql_database); //打开数据库 $sql ="select * from news "; //SQL语句 $result = mysql_query($sql,$conn); //查询 ?>
3. Read the contents of the table, here we Use while, you can use for or other according to the specific situation.
<?php while($row = mysql_fetch_array($result)) { echo "<div style=\"height:24px; line-height:24px; font-weight:bold;\">"; //排版代码 echo $row['Topic'] . "<br/>"; echo "</div>"; //排版代码 } ?>
4.php writes to the database, Mysql data writing
<?php $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password); //连接数据库 mysql_query("set names 'utf8'"); //数据库输出编码 mysql_select_db($mysql_database); //打开数据库 $sql = "insert into messageboard (Topic,Content,Enabled,Date) values ('$Topic','$Content','1','2011-01-12')"; mysql_query($sql); mysql_close(); //关闭MySQL连接 ?>