

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);

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools