PHP操作MySQL的三种方式

  1. MySQL:非永久链接,性能比较低,PHP5.5以后被废弃
  2. MySQLi:永久链接,减轻服务器压力,只支持MySQL数据库,并且会导致内存的浪费
  3. PDO:能实现MySQLi的常用功能,支持大部分数据库

MySQL面向对象操作数据库

1、面向对象方式链接数据库

$mysqli = new mysqli('host','username','password','database');

2、执行SQL语句

$mysqli -> query($sql);

3、获取结果集

$result -> fetch_array();

实例

<?php
header("content-type:text/html;charset=utf-8");
$mysqli = new mysqli('127.0.0.1','root','root','mysqli');
$mysqli -> query('set names utf8' );//设置字符集utf-8
//添加数据
//$result = $mysqli -> query("INSERT INTO users(name,money) VALUES('jerry',50)");

//修改数据
//$result = $mysqli -> query("UPDATE users SET money=money+10 WHERE id = 3");

//删除数据
//$result = $mysqli -> query("DELETE FROM users WHERE id = 3");

//查询数据
$result = $mysqli -> query("SELECT * FROM users");
$data = $result -> fetch_all(MYSQLI_ASSOC);
var_dump($data);