What is the method to connect to the database in php?

青灯夜游
Release: 2023-02-27 10:04:01
Original
3909 people have browsed it

In the PHP development process, we need to connect to the database to develop dynamic websites, so how to connect to the database? The following article will introduce you to some methods of connecting to the database in PHP. I hope it will be helpful to you.

What is the method to connect to the database in php?

Common methods to connect to the database in php

Mysql_connect

This is the database connection method we first encountered when we first started learning PHP. The connection result can be returned through the mysql_connect() function, and a MySQL connection ID is returned. If it fails, FALSE is returned. , so subsequent operations can be performed.

Code example

<?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);
?>
Copy after login

Note

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

Mysqli

This is an object-oriented database connection method. You need to instantiate an object before connecting, and then perform database operations through this object

Code example

<?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());
  }
Copy after login

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

PDO

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

代码示例
<?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();
            }
?>
Copy after login

Summary

There are three ways to connect to the database. As for which one to choose, just Depends on the pros and cons of each method.

mysql_connect()

Design and develop early extensions that allow PHP applications to interact with MySQL databases. The mysql extension provides a process-oriented interface; and it is a short connection. When connecting to the database multiple times, multiple processes need to be created.

mysqli

Mysqli extension has a series of advantages. Compared with the mysql extension, the main improvements are: object-oriented interface, prepared statement support, multi-statement execution support, transaction support, enhanced debugging capabilities, Embedded service support.

pdo

PHP data object is a database abstraction layer specification in PHP applications. PDO provides a unified API interface so that your PHP application does not care about the specific database server system type to be connected. In other words, if you use PDO's API, you can seamlessly switch database servers whenever needed.

The above is the detailed content of What is the method to connect to the database in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!