search
HomeBackend DevelopmentPHP ProblemThere are several ways to modify the database in PHP

There are several ways to modify the database in PHP

There are several ways to modify the database in PHP

In the PHP development process, we need to connect to the database to develop dynamics There are usually four ways to connect to a database on a website: Mysql-connect(),Mysqli,PDO,ADODB, we can use this How to connect to the database in 4 to modify the database will be introduced one by one next.

Mysql_connect

This is the first database connection method we encountered when we first started learning PHP. The connection result can be returned through the mysql_connect() function, then a MySQL connection identifier, returns FALSE if failed. Therefore, subsequent database modification operations can be performed.

 <?php
  $con=mysql_connect("localhost" ,"root","password")
  if($con){
    mysql_select_db("db_name",$con);
    $sql="select * from table_name where id=1";
    $result=mysql_query($sql);
    while($row=mysql_fetch_row($result)){
        echo  "$row";
    }
  }else{
    die("无法连接数据库".mysql_error());
  }
  mysql_close($con);
  ?>

This connection method is a short connection, not a long connection. If the connection is long, use mysql_pconnectct()

Mysqli

This is a The object-oriented database connection method requires instantiating an object before connecting, and then performing database operations through this object.

<?php
    $con=new mysqli("localhost","root","password","db_name");
    if(!mysqli_connect_error()){
        $sql="select * from table_name where id=1";
        $result=$con->query($sql);
    while($row=$result->fetch_row($result)){
        echo  "$row";
    }
      }else{
        die("无法连接数据库".mysql_error());
      }

mysqli is a long connection method and is more secure than mysql_connect

PDO

What is pdo, pdo is added to the php5 center The way to connect to the database,

 <?php
        $pdo=new      
        PDO(&#39;mysql:host=localhost;dbname=db_name&#39;,"root",
        $sql="select * from table_name where id=1";
       "password");
        try{
              $result=$pdo->query($sql);
              foreach($result as $k){
                    print_r($k);
              }
            }catch(PDOException $e){
                       echo $e->getMessage();
            }
    ?>

ADODB

No matter what the back-end database is, the way to access the database is the same, and developers don’t have to settle for a certain set of Database, you must learn another set of different access methods, which greatly reduces the knowledge burden of developers. Past knowledge can still be used in the future

require_once &#39;./adodb5/adodb.inc.php&#39;;
  $conn = &ADONewConnection(&#39;mysql&#39;);
  $conn->connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;,&#39;test&#39;);
  $conn->Execute("set names utf8");
  $res = $conn->Execute("select * from user");
  if (!$res){
     echo $conn->ErrorMsg();
  }else{
     var_dump($res);
   }

For more PHP related knowledge, please visitPHP中文网!

The above is the detailed content of There are several ways to modify the database in PHP. 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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)