Database connection steps
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
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數(shù)據(jù)庫(kù)服務(wù)器 $mysql_username='root'; //改成自己的mysql數(shù)據(jù)庫(kù)用戶(hù)名 $mysql_password='123456789'; //改成自己的mysql數(shù)據(jù)庫(kù)密碼 $mysql_database='php'; //改成自己的mysql數(shù)據(jù)庫(kù)名 ?>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") ; //連接數(shù)據(jù)庫(kù) mysql_query("set names 'utf8'"); //數(shù)據(jù)庫(kù)輸出編碼 應(yīng)該與你的數(shù)據(jù)庫(kù)編碼保持一致.南昌網(wǎng)站建設(shè)公司百恒網(wǎng)絡(luò)PHP工程師建議用UTF-8 國(guó)際標(biāo)準(zhǔn)編碼. mysql_select_db($mysql_database); //打開(kāi)數(shù)據(jù)庫(kù) $sql ="select * from news "; //SQL語(yǔ)句 $result = mysql_query($sql,$conn); //查詢(xún) ?>
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); //連接數(shù)據(jù)庫(kù) mysql_query("set names 'utf8'"); //數(shù)據(jù)庫(kù)輸出編碼 mysql_select_db($mysql_database); //打開(kāi)數(shù)據(jù)庫(kù) $sql = "insert into messageboard (Topic,Content,Enabled,Date) values ('$Topic','$Content','1','2011-01-12')"; mysql_query($sql); mysql_close(); //關(guān)閉MySQL連接 ?>