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.
<?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); ?>
<?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); ?>
<?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); ?>
<?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 <?php 2 var_dump($_GRT); 3 ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to read the first few records in a database using PHP? When developing web applications, we often need to read data from the database and display it to the user. Sometimes, we only need to display the first few records in the database, not the entire content. This article will teach you how to use PHP to read the first few records in the database and provide specific code examples. First, assume that you have connected to the database and selected the table you want to operate on. The following is a simple database connection example:

In Java programs, connecting to the database is a very common operation. Although ready-made class libraries and tools can be used to connect to the database, various abnormal situations may still occur during program development, among which SQLException is one of them. SQLException is an exception class provided by Java. It describes errors that occur when accessing the database, such as query statement errors, table non-existence, connection disconnection, etc. For Java programmers, especially those using JDBC (Java Data

Go language connects to the database by importing the database driver, establishing a database connection, executing SQL statements, using prepared statements and transaction processing. Detailed introduction: 1. Import the database driver and use the github.com/go-sql-driver/mysql package to connect to the MySQL database; 2. Establish a database connection and provide the database connection information, including the database address, user name, password, etc. Establish a database connection and so on through the sql.Open function.

Using Go language to connect to the database: Improving application performance and efficiency As applications develop and the number of users increases, data storage and processing become more and more important. In order to improve the performance and efficiency of applications, properly connecting and operating the database is a crucial part. As a fast, reliable, and highly concurrency development language, Go language has the potential to provide efficient performance when processing databases. This article will introduce how to use Go language to connect to the database and provide some code examples. Install database driver using Go language

Steps and techniques for implementing product inventory in PHP In the e-commerce industry, product inventory management is a very important task. Timely and accurate inventory counting can avoid sales delays, customer complaints and other problems caused by inventory errors. This article will introduce the steps and techniques on how to use PHP to implement product inventory counting, and provide code examples. Step 1: Create a database First, we need to create a database to store product information. Create a database named "inventory" and then create a database named "prod

Learn Go language: basic knowledge of connecting to databases, specific code examples are required. Go language is an open source programming language. Its simple and efficient features make it loved and used by more and more developers. During the development process, it is often necessary to establish a connection with the database to perform operations such as reading, writing, updating, and deleting data. Therefore, learning how to connect to a database in Go language is a very important skill. Database driver In the Go language, a database driver is required to connect to the database. At present, the main database drivers of Go language are:

Installing and configuring PHP on Ubuntu systems to connect to MSSQL databases is a common task, especially when developing web applications. In this article, we will introduce how to install PHP, MSSQL extensions and configure database connections on Ubuntu systems, while providing specific code examples. Step 1: Install PHP and MSSQL extensions Install PHP First, you need to make sure that PHP is installed on your Ubuntu system. PHP can be installed with the following command: sudoaptu

Title: What are the important functions of MySQL’s Jar package? MySQL is a popular relational database management system that many Java developers use when developing applications. In order to interact with the MySQL database in a Java project, the official Java driver Jar package provided by MySQL is usually used. MySQL's Jar package has many important functions. This article will introduce some of them and provide specific code examples. 1. Connect to MyS
