Home > Article > Backend Development > How to connect to database in php
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('localhost','root',''); var_dump($link); //1、连接数据库 if (!$link) { exit('连接数据库失败');} //2、判断数据库是否连接成功 mysqli_set_charset($link,'utf8'); //3、设置字符集 mysqli_select_db($link,'bbs'); //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、关闭数据库 ?>
This returns an associated array.
##Output all arrays: (use loop)<?php $link = mysqli_connect('localhost','root',''); if (!$link) { exit($'连接数据库失败');} mysqli_set_charset($link,'utf8'); mysqli_select_db($link,'bbs'); $sql = "select * from bbs_user"; $res = mysqli_query($link,$sql); while ($result = mysqli_fetch_assoc($res)) { var_dump($result);} mysqli_close($link); ?>
<?php $link = mysqli_connect('localhost','root',''); if (!$link) { exit('连接数据库失败');} mysqli_set_charset($link,'utf8'); mysqli_select_db($link,'bbs'); $sql = "select * from bbs_user"; $res = mysqli_query($link,$sql); $result = mysqli_fetch_row($res); var_dump($result); mysqli_close($link); ?>That is, output an associative array and an index array:
<?php $link = mysqli_connect('localhost','root',''); if (!$link){ exit('连接数据库失败');} mysqli_set_charset($link,'utf8'); mysqli_select_db($link,'bbs'); $sql = "select * from bbs_user"; $res = mysqli_query($link,$sql); $result = mysqli_fetch_array($res); var_dump($result); mysqli_close($link); ?>To query the total number of data:
<?php $link = mysqli_connect('localhost','root',''); if (!$link) { exit('连接数据库失败');} mysqli_set_charset($link,'utf8'); mysqli_select_db($link,'bbs'); $sql = "select * from bbs_user"; $obj = mysqli_query($link,$sql); $res = mysqli_num_rows($obj); var_dump($res); mysqli_close($link); ?>Insert new data using php:
<?php $link = mysqli_connect('localhost','root',''); if (!$link) { exit('连接数据库失败');} mysqli_set_charset($link,'utf8'); mysqli_select_db($link,'bbs'); $sql = "insert into bbs_user values(9,'kkk','789789','nanjian',2,15)"; $obj = mysqli_query($link,$sql); $res = mysqli_insert_id($link); var_dump($res); mysqli_close($link); ?>
<?php $link = mysqli_connect('lcoalhost','root',''); if (!$link) { exit('链接数据库失败');} mysqli_set_charset($link,'utf8';) mysqli_select_db($link,'bbs'); $sql = "select * from bbs_user"; $obj = mysqli_query($link,$sql); echo '<th>编号</th><th>用户名</th><th>地址</th><th>性别</th><th>年龄</th>'; while ($res = mysqli_fetch_assoc($obj)) { echo '<tr>'; echo '<td>'.$res['id'].'</td>'; echo '<td>'.$res['username'].'</td>'; echo '<td>'.$res['address'].'</td>'; echo '<td>'.$res['sex'].'</td>'; echo '<td>'.$res['age'].'</td>'; echo '<td><a href="del.php?id='.$res['id'].'">删除</a>/<a href="update.php?id='.$res['id'].'">修改</a></td>'; echo '</tr>';} ?>
##Compile the deleted php file: (del.php)
<?php $id=$_GET['id']; $link = mysqli_connect('localhost','root',''); if (!$link) { exit('连接数据库失败');} mysqli_set_charset($link,'utf8'); mysqli_select_db($link,'bbs'); $sql = "delete from bbs_user where id=$id"; $boolearn = mysqli_query($link,$sql); if ($boolearn && msyqli_affected_rows($link)) { echo '删除成功';} else { echo '删除失败';} mysqli_close($link); ?>
Compile the modified php file: (update.php)
<?php $id = $_GET['id']; $link = mysqli_connect('localhost','root',''); if (!$link) { exit('连接数据库失败');} mysqli_set_charset($link,'utf8'); msyqli_select_db($link,'bbs'); $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=['username'] ?>" name="username"/><br /> 地址:<input type="text" value="<?php $rows=['address'] ?>" name="address" /><br /> 性别:<input type="text" value="<?php $rows=['sex'] ?>" name="sex" /> <br /> 年龄:<input type="text" value="<?php $row=['age']>" name="age" /> <input type="submit" value="执行修改" /> </form> </html>
doupdate.php:
1 39345467f72bc0a7d08ebeab91218ef1
doupadate.php
<?php $id = $_GET['id']; $username = $_GET['username']; $address = $_GET['adress']; $sex = $_GET['sex']; $age = $_GET['age']; $link = mysqli_connect('lcoalhost','root',''); if (!$link) { exit('数据库连接失败');} mysqli_set_charset($link,'utf8'); mysqli_select_db($link,'bbs'); $sql = "update bbs_user set username='$username', address='$address', sex='$sex', age='$age' where id='$id'"; $res = mysqli_query($link,$sql); if ($res && mysqli_affected_rows($link)) { echo '修改成功<a href="update.php">返回</a>';} else { echo '修改失败';} mysqli_close($link); ?>
## 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!