Home>Article> PHP connects to the database and implements the addition, deletion, modification and query functions of the database

PHP connects to the database and implements the addition, deletion, modification and query functions of the database

无忌哥哥
无忌哥哥 Original
2018-06-27 14:59:58 2131browse

mySQLi database connection: (only applicable to PHP5 and above, please use mySQL to connect after PHP5)

1. Process-oriented method

//连接数据库,参数分别为本地(localhost),用户名(默认是root),密码(默认是空),数据库名(你要连接的数据库名称)。 $link = mysqli_connect('localhost','root','','mydb'); if(mysqli_errno($link)){ //检测数据库是否连接成功,如果连接失败输出错误,否则数据库连接成功 echo mysqli_error($link); }else{ echo '连接数据库成功'; }

2. Object-oriented method

$mysqli = new mysqli('localhost','root','','mydb'); if($mysqli->errno){ echo $mysqli->error; }else{ echo'连接数据库成功'; }

Add, delete, modify and query the database through the constructor:

1. Add data to the database

function add($sql){ $mysqli = new mysqli('localhost','root','','mydb'); mysqli_set_charset($mysqli,'utf8'); //设置编码格式 让数据库支持中文 if($mysqli->errno){ echo $mysqli->error; }else{ echo '连接数据库成功'; } $res = $mysqli->query($sql); if($res){ return true; }else{ return false; } }

Example usage: Add data to the table indicated as users

$name = "马云"; $age = 38; $pwd = 123789; $sql = "INSERT INTO users (username,age,pwd) VALUES ('$name',$age,$pwd)"; $res = add($sql); if($res){ //检测是否添加成功 echo '添加成功'; }else{ echo '添加失败'; }

2. Update database:

function update($sql){ $link =mysqli_connect('localhost','root','','mydb'); mysqli_set_charset($link,'utf8'); if(mysqli_errno($link)){ echo mysqli_error($link); }else{ echo'连接数据库成功'; }; $res =mysqli_query($link,$sql); if($res){ return true; }else{ return false; } }

Example usage: add data to the table named users

$sql = "UPDATE users SET username='王健林' WHERE id=23"; $res = update($sql); if($res){ echo'更新成功'; }else{ echo'更新失败'; }

3. Delete (use update instead of delete)

function delete($sql){ $link =mysqli_connect('localhost','root','','mydb'); mysqli_set_charset($link,'utf8'); if(mysqli_errno($link)){ echo mysqli_error($link); }else{ echo'连接数据库成功'; }; $res =mysqli_query($link,$sql); if($res){ return true; }else{ return false; } }

Example usage:

$sql ="DELETE FROM users WHERE id=23"; $res = delete($sql); if($res){ echo'删除成功'; }else{ echo'删除失败'; }

Query:

function getlist($sql){ $link =mysqli_connect('localhost','root','','mydb'); mysqli_set_charset($link,'utf8'); if(mysqli_errno($link)){ echo mysqli_error($link); }else{ echo'连接数据库成功'; }; $res =mysqli_query($link,$sql); // 返回查询到的所有数据 // $list = mysqli_fetch_all($res); //常用 while($list =mysqli_fetch_assoc($res)){ $arr[] = $list; } return $arr; }

Example usage:

$sql ="SELECT * FROM users"; $res = getlist($sql); print_r($res);
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