Home  >  Article  >  Database  >  PHP connects to the database and implements the most basic addition, deletion, modification and query operations through a process-oriented approach.

PHP connects to the database and implements the most basic addition, deletion, modification and query operations through a process-oriented approach.

jacklove
jackloveOriginal
2018-06-08 10:39:453386browse

Add, delete, modify and query are the most basic and important in database operations.

After we have set up the PHP environment, let's take a look at how to connect to the MySQL database through PHP and how to implement the most basic additions, deletions, modifications and queries. Not much else to say, let’s go directly to the code.

Recommended related mysql video tutorials: "mysql tutorial"

1. Connect MySQL database with PHP

<?php
    $host ="localhost";//服务器地址
    $root ="root";//用户名
    $password ="admin";//密码
    $database ="beyondweb_test";//数据库名               
    $conn = mysql_connect($host,$root,$password);//连接数据库
    if(!$conn){
        die("数据库连接失败!".mysql_error());
    }else{
        echo"数据库连接成功";
    }                   
    mysql_select_db($database,$conn);//选择数据库
    mysql_query("set names utf-8");//设置编码为utf-8
?>

2. Add to the database Data

First of all, I created such a user table in the beyondweb_test database for demonstration. As shown in the figure:

SQL statement:

CREATE TABLE user(
    id INT(20) PRIMARY KEY AUTO_INCREMENT,
    nikename VARCHAR(30),
    email VARCHAR(40)
);

Then add data to the database:

<?php
    $query ="INSERT INTO user(nikename,email) VALUES(#张三#,#beyondwebdotcn@xxx.com#);";//#号代表单引号,使用单引号才是正确的
                   
    $result = mysql_query($query);
                   
    if(!$result){
        echo"Error!";
    }else{
        echo"Success!";
        mysql_close($conn);//关闭数据库连接
    }
?>

3. Modify the data in the database

We are also based on To operate the user table, for example, change "Zhang San" to "Li Si", the php code is as follows:

<?php
    $query ="UPDATE user SET nikename=#李四# WHERE id=#1#;";//#号代表单引号,使用单引号才是正确的              
    $result = mysql_query($query);              
    if(!$result){
        echo"Error!";
    }else{
        echo"Success!";
        mysql_close($conn);//关闭数据库连接
    }
?>

In fact, the SQL statement has changed, and the others are exactly the same.

4. Query the database

Just change the SQL statement. For example, query all the data in the table and output it in table form:

<?php
    $query ="SELECT * FROM user;";
            
    $result = mysql_query($query);
            
    if(!$result){
        echo"Error!";
    }else{
        echo"Success!";
    }         
?>
    <br/>
    <table border="1px">
        <tr>
            <th>id</th>
            <th>nikename</th>
            <th>email</th>
        </tr>
<?php
    while($row = mysql_fetch_row($result)){
        echo"<tr>";
        echo"<td>".$row[0]."</td>";
        echo"<td>".$row[1]."</td>";
        echo"<td>".$row[2]."</td>";
        echo"</tr>";
    }
?>
    </table>
<?php           
    mysql_close($conn);      
?>

5. Delete data

The solutions to adding data, modifying data, and deleting data have been given before. Then the only "delete" left in "add, delete, modify, and check" is "delete". Let's take a look at how to delete data. In fact, it is the same as "add, delete, modify, check". The above is almost the same, it only takes one sentence, just change the SQL statement. Let’s look at the PHP code:

<?php
    $query ="DELETE FROM user WHERE nikename=#张三#;";//#号代表单引号,使用单引号才是正确的           
    $result = mysql_query($query);          
    if(!$result){
        echo"Error!";
    }else{
        echo"Success!";
        mysql_close($conn);//关闭数据库连接
    }
?>

Okay, the above is the PHP connection to the database and the most basic addition, deletion, modification and query operations. I hope it can be helpful to beginners.

But the above are all handled with a process-oriented approach, which is very complicated. If you are interested, you can follow the php Chinese website:

Related recommendations:

How to implement Mysql database connection, query, record set and other operations through PHP

How to set the MySQL time zone

MYSQL query time data


The above is the detailed content of PHP connects to the database and implements the most basic addition, deletion, modification and query operations through a process-oriented approach.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn