search
HomeBackend DevelopmentPHP7PHP7 performs database operations (connection, addition, deletion, modification and query operations)

PHP7 performs database operations (connection, addition, deletion, modification and query operations)

Update
mysqli connection, recommended

$conn = mysqli_connect('127.0.0.1','root2','root2');
mysqli_select_db($conn,'jianshu');
$sql = "select * from posts";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)){
var_dump($row);}

Recommended (free):php7

Handle errors

mysqli_connect_errno()错误代码
mysqli_connect_error()错误内容
if (!$link) {
exit('error('.mysqli_connect_errno().'):'.mysqli_connect_error());//不继续向下执行
//die
}

Set character set

mysqli_set_charset($conn,'utf8');

Value

mysqli_fetch_all
mysqli_fetch_array
mysqli_fetch_assoc
mysqli_fetch_num

//mysqli_fetch_array默认返回MYSQLI_BOTH
//MYSQLI_ASSOC、 MYSQLI_NUM 、MYSQLI_BOTH默认
//[0] => 34 [id] => 34 [1] =>Linux常用技巧 [title] => Linux常用技巧
print_r(mysqli_fetch_all($result));

mysqli_fetch_array//默认返回两种MYSQLI_BOTH
//MYSQLI_ASSOC MYSQLI_NUM MYSQLI_BOTH默认
//[0] => 34 [id] => 34 [1] =>Linux常用技巧 [title] => Linux常用技巧
print_r(mysqli_fetch_array($result,MYSQLI_NUM));

mysqli_fetch_num
//获取查询结果中的一条数据,为索引数组(数据库第一条,不一定是最小或最大id)
//执行后,指向下一条数据
// [0] => 34 [1] => Linux常用技巧 [2] => 28echo
print_r(mysqli_fetch_row($result));

mysqli_fetch_assoc
//获取查询结果中的一条数据,为关联数组(数据库第一条,不一定是最小或最大id)
//执行后,指向下一条数据
// [id] => 34[title] => Linux常用技巧 [read] => 28echo print_r(mysqli_fetch_assoc($result));

Close resources, close database

mysqli_free_result($result);//释放结果资源
mysqli_close($conn); //关闭数据库连接

mysql connection (not recommended)

$link = mysql_link('localhost','root','123') or die('error');
my_select_db('user',$link) or die('error');
$sql = "";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result)){
echo $row['id'];
}




Previous

1. Connection (mysqli method)

$con = new mysqli("localhost", "username", "password", "databasename");

2. Query

	$con->query('set names utf8;'); 
	$sql = "SELECT * FROM tablename";  
	$result = $con->query($sql);  
	$data=array();
	while ($tmp=mysqli_fetch_assoc($result)) {
		$data[]=$tmp;
	}
	var_dump($data);

3 , insert

	$con->query('set names utf8;');
    $sql="INSERT INTO tablename (name,telphone) VALUES ('name','telphone')";
	if($result = $con->query($sql)){
        echo "成功";
	}else{
		echo "失败";
	}

The above is the detailed content of PHP7 performs database operations (connection, addition, deletion, modification and query operations). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools