Home  >  Article  >  Topics  >  What is the function method for php to connect to mysql database?

What is the function method for php to connect to mysql database?

青灯夜游
青灯夜游Original
2021-07-06 18:10:3210609browse

The function that PHP uses to connect to the mysql database is mysqli_connect(). This function can open a new connection to the MySQL server. The syntax format is "mysqli_connect(hostname, username, password, database, [port], [ socket]);".

What is the function method for php to connect to mysql database?

The operating environment of this tutorial: windows7 system, PHP7.1&&mysql8 version, DELL G3 computer

In PHP, you can use the mysqli_connect extension of mysqli () function to realize the connection to the MySQL database. The function syntax format is as follows:

mysqli_connect(host,username,password,dbname,port,socket);
Parameter Description
host Optional. Specify the hostname or IP address.
username Optional. Specifies the MySQL username.
password Optional. Specifies the MySQL password.
dbname Optional. Specifies the database to use by default.
port Optional. Specifies the port number to attempt to connect to the MySQL server.
socket Optional. Specifies the socket or named pipe to use.

Return value: Returns an object representing the connection to the MySQL server.

It should also be noted that the mysqli_connect() function is an alias of the mysqli::__construct() function, and all objects using mysqli() can also be used to connect to the database.

Example: The following is a simple code to connect to the database.

1) Process-oriented writing method

<?php
    $host     = &#39;localhost&#39;;
    $username = &#39;root&#39;;
    $password = &#39;root&#39;;
    $dbname   = &#39;test&#39;;
    $port     = &#39;3306&#39;;
    $link     = @mysqli_connect($host,$username,$password,$dbname,$port);   // 连接到数据库
    if($link){
        mysqli_set_charset($link,&#39;UTF-8&#39;);      // 设置数据库字符集
        $sql    = &#39;select * from user&#39;;         // SQL 语句
        $result = mysqli_query($link, $sql);    // 执行 SQL 语句,并返回结果
        $data   = mysqli_fetch_all($result);    // 从结果集中获取所有数据
        mysqli_close($link);
    }else{
        die(&#39;数据库连接失败!&#39;);
    }
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    print_r($data);
?>

2) Object-oriented writing method

<?php
    $host     = &#39;localhost&#39;;
    $username = &#39;root&#39;;
    $password = &#39;root&#39;;
    $dbname   = &#39;test&#39;;
    $mysql    = new Mysqli($host, $username, $password, $dbname);
    if($mysql -> connect_errno){
        die(&#39;数据库连接失败:&#39;.$mysql->connect_errno);
    }else{
        $mysql -> set_charset(&#39;UTF-8&#39;); //  设置数据库字符集
        $sql = &#39;select * from user&#39;;         // SQL 语句
        $result = $mysql -> query($sql);
        $data = $result -> fetch_all();
        $mysql -> close();
    }
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    print_r($data);
?>

The running results are as follows:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 张三
        )
)

Recommended learning : "PHP Video Tutorial"

The above is the detailed content of What is the function method for php to connect to mysql database?. 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