How to connect to database in php

不言
Release: 2023-04-03 09:38:01
Original
17114 people have browsed it

This article introduces to you how to connect to the database in PHP. It has certain reference value. Friends in need can refer to it.

1. PHP link database:

1. Link database

2. Determine whether the connection is successful

3. Set characters Set

4. Select database

5. Prepare SQL statement

6. Send SQL statement

7. Process result set

8. Release resources (close the database)

 $result = mysqli_querry($link,$sql) //Return an object

 mysqli_fetch_assoc($result) Read down one by one, when returning A one-dimensional associative array

Mysqli_fetch_row($result) returns an indexed array

mysqli_fetch_array($result) returns an indexed and associated array

mysqli_num_rows($result ) Returns the total number of results in the query

Mysqli_affected_rows($link) Returns the number of rows affected when you modify, delete, or add

mysqli_insert_id($link) Return is the auto-incremented id of the current data you inserted

<<?php
    $link = mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
    var_dump($link);
    //1、连接数据库
    if (!$link) {
    exit(&#39;连接数据库失败&#39;);}
    //2、判断数据库是否连接成功
    mysqli_set_charset($link,&#39;utf8&#39;);
    //3、设置字符集
    mysqli_select_db($link,&#39;bbs&#39;);
    //4、选择数据库
    $sql = "select * from bbs_user";
    //5、准备sql语句
    $res = mysqli_query($link,$sql);
    //6、发送sql语句
    $result = mysqli_fetch_assoc($res);
    $result = mysqli_fetch_assoc($res);
    //7、处理结果集
    mysqli_close($link);
    //8、关闭数据库
?>
Copy after login

This returns an associated array.

##Output all arrays: (use loop)

<?php
    $link = mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
    if (!$link) {
    exit($&#39;连接数据库失败&#39;);}
    mysqli_set_charset($link,&#39;utf8&#39;);
    mysqli_select_db($link,&#39;bbs&#39;);
    $sql = "select * from bbs_user";
    $res = mysqli_query($link,$sql);
    while ($result = mysqli_fetch_assoc($res)) {
    var_dump($result);}
    mysqli_close($link);
?>
Copy after login


Output an index array:

<?php
    $link = mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
    if (!$link) {
    exit(&#39;连接数据库失败&#39;);}
    mysqli_set_charset($link,&#39;utf8&#39;);
    mysqli_select_db($link,&#39;bbs&#39;);
    $sql = "select * from bbs_user";
    $res = mysqli_query($link,$sql);
    $result = mysqli_fetch_row($res);
     var_dump($result);
    mysqli_close($link);
?>
Copy after login

That is, output an associative array and an index array:

<?php
    $link = mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
    if (!$link){
    exit(&#39;连接数据库失败&#39;);}
    mysqli_set_charset($link,&#39;utf8&#39;);
    mysqli_select_db($link,&#39;bbs&#39;);
    $sql = "select * from bbs_user";
    $res = mysqli_query($link,$sql);
    $result = mysqli_fetch_array($res);
     var_dump($result);
    mysqli_close($link);
?>
Copy after login

To query the total number of data:

<?php
    $link = mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
    if (!$link) {
    exit(&#39;连接数据库失败&#39;);}
    mysqli_set_charset($link,&#39;utf8&#39;);
    mysqli_select_db($link,&#39;bbs&#39;);
    $sql = "select * from bbs_user";
    $obj = mysqli_query($link,$sql);
    $res = mysqli_num_rows($obj);
    var_dump($res);
    mysqli_close($link);
?>
Copy after login

Insert new data using php:

<?php
    $link = mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
    if (!$link) {
    exit(&#39;连接数据库失败&#39;);}
    mysqli_set_charset($link,&#39;utf8&#39;);
    mysqli_select_db($link,&#39;bbs&#39;);
    $sql = "insert into bbs_user values(9,&#39;kkk&#39;,&#39;789789&#39;,&#39;nanjian&#39;,2,15)";
    $obj = mysqli_query($link,$sql);
    $res = mysqli_insert_id($link);
    var_dump($res);
    mysqli_close($link);
?>
Copy after login


<?php
    $link = mysqli_connect(&#39;lcoalhost&#39;,&#39;root&#39;,&#39;&#39;);
    if (!$link) {
    exit(&#39;链接数据库失败&#39;);}
    mysqli_set_charset($link,&#39;utf8&#39;;)
    mysqli_select_db($link,&#39;bbs&#39;);
    $sql = "select * from bbs_user";
    $obj = mysqli_query($link,$sql);
    echo &#39;<th>编号</th><th>用户名</th><th>地址</th><th>性别</th><th>年龄</th>&#39;;
    while ($res = mysqli_fetch_assoc($obj)) {
    echo &#39;<tr>&#39;;
       echo &#39;<td>&#39;.$res[&#39;id&#39;].&#39;</td>&#39;;   
       echo &#39;<td>&#39;.$res[&#39;username&#39;].&#39;</td>&#39;;
       echo &#39;<td>&#39;.$res[&#39;address&#39;].&#39;</td>&#39;;
       echo &#39;<td>&#39;.$res[&#39;sex&#39;].&#39;</td>&#39;;
       echo &#39;<td>&#39;.$res[&#39;age&#39;].&#39;</td>&#39;;
       echo &#39;<td><a href="del.php?id=&#39;.$res[&#39;id&#39;].&#39;">删除</a>/<a href="update.php?id=&#39;.$res[&#39;id&#39;].&#39;">修改</a></td>&#39;;
    echo &#39;</tr>&#39;;}        
?>
Copy after login

##Compile the deleted php file: (del.php)

<?php
    $id=$_GET[&#39;id&#39;];
    $link = mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
    if (!$link) {
    exit(&#39;连接数据库失败&#39;);}
    mysqli_set_charset($link,&#39;utf8&#39;);
    mysqli_select_db($link,&#39;bbs&#39;);
    $sql = "delete from bbs_user where id=$id";
    $boolearn = mysqli_query($link,$sql);
    if ($boolearn && msyqli_affected_rows($link)) {
    echo &#39;删除成功&#39;;} else {
    echo &#39;删除失败&#39;;}
    mysqli_close($link);
?>
Copy after login

Compile the modified php file: (update.php)

<?php
    $id = $_GET[&#39;id&#39;];
    $link = mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
    if (!$link) {
    exit(&#39;连接数据库失败&#39;);}
    mysqli_set_charset($link,&#39;utf8&#39;);
    msyqli_select_db($link,&#39;bbs&#39;);
    $sql = "select * from bbs_user where id=$id";
    $obj = mysqli_query($link,$sql);
    $rows = mysqli_fetch_assoc($obj);
?>
<html>
    <form action =" doupdate.php">
            <input type="hidden" value="<?php echo $id;?>" name="id" />
            用户名:<input type="text" value="<?php $rows=[&#39;username&#39;] ?>" name="username"/><br />
            地址:<input type="text" value="<?php $rows=[&#39;address&#39;] ?>" name="address" /><br />
            性别:<input type="text" value="<?php $rows=[&#39;sex&#39;] ?>" name="sex" />
<br />
            年龄:<input type="text" value="<?php $row=[&#39;age&#39;]>" name="age" />
            <input type="submit" value="执行修改" /> 
    </form>
</html>
Copy after login

doupdate.php:

1 <?php
2     var_dump($_GRT);
3 ?>
Copy after login

doupadate.php

<?php
    $id = $_GET[&#39;id&#39;];
    $username = $_GET[&#39;username&#39;];
    $address = $_GET[&#39;adress&#39;];
    $sex = $_GET[&#39;sex&#39;];
    $age = $_GET[&#39;age&#39;];
    $link = mysqli_connect(&#39;lcoalhost&#39;,&#39;root&#39;,&#39;&#39;);
    if (!$link) {
    exit(&#39;数据库连接失败&#39;);}
    mysqli_set_charset($link,&#39;utf8&#39;);
    mysqli_select_db($link,&#39;bbs&#39;);
    $sql = "update bbs_user set username=&#39;$username&#39;, address=&#39;$address&#39;, 
    sex=&#39;$sex&#39;, age=&#39;$age&#39; where id=&#39;$id&#39;";
    $res = mysqli_query($link,$sql);
    if ($res && mysqli_affected_rows($link)) {
    echo &#39;修改成功<a href="update.php">返回</a>&#39;;}
    else {
    echo &#39;修改失败&#39;;}
    mysqli_close($link);
?>
Copy after login

## Related recommendations:

How to delete a directory in PHP Defined function

How to use PHP to write a simple interpreter

The above is the detailed content of How to connect to 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