一、mysql链接数据库:(本扩展自 PHP 5.5.0 起已废弃,并在将来会被移除。应使用 MySQLi 或 PDO_MySQL 扩展)
$con=mysql_connect('localhost','root','123456');
if(!$con){
die("could not connect to the database: " . mysql_error());//诊断连接错误
}
mysql_set_charset('utf8');
mysql_select_db('表名');
增:
$sql="INSERT INTO `表名`(`字段名`,`字段名`) VALUES('{变量}','{变量}')";
$re=mysql_query($sql);
$id=mysql_insert_id();
if($re){
echo '添加成功'.'添加的数据库ID为'.$id;
}else{
echo '添加失败';
}
删:
$sql="DELETE FROM `表名` WHERE id='{条件变量}'";//删除一定要书写条件,防止误删
$re=mysql_query($sql);
if($re){
echo '删除成功';
}else{
echo '删除失败';
}
改:
$sql="UPDATE `表名` SET `字段名`='{变量}' WHERE `id`='{条件变量}'";
$re=mysql_query($sql);
if($re){
echo '修改成功';
}else{
echo '修改失败';
}
查:
$sql="SELECT * FROM `表名` WHERE `id`='{条件变量}'";
//获取一条,一维数组
$result=mysql_query($sql);
$data=mysql_fetch_array($result,MYSQL_ASSOC);
//获取多条,二维数组
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$data[]=$res;
}
return $data;
二、mysqli链接数据库:
1、面向过程
$con=mysqli_connect('localhost','root','123456','data');
if(!$con){
die( "连接错误: " . mysqli_connect_error());
}
mysqli_set_charset($con,'utf8');
增:
$sql="INSERT INTO `表名`(`字段名`,`字段名`) VALUES('{变量}','{变量}')";
$re=mysqli_query($con,$sql);
$id=mysqli_insert_id($con);
if($re){
echo '添加成功'.'添加的数据库ID为'.$id;
}else{
echo '添加失败';
}
删:
$sql="DELETE FROM `表名` WHERE id='{条件变量}'";//删除一定要书写条件,防止误删
$re=mysqli_query($con,$sql);
if($re){
echo '删除成功';
}else{
echo '删除失败';
}
改:
$sql="UPDATE `表名` SET `字段名`='{变量}' WHERE `id`='{条件变量}'";
$re=mysqli_query($con,$sql);
if($re){
echo '修改成功';
}else{
echo '修改失败';
}
查:
$sql="SELECT * FROM `表名` WHERE `id`='{条件变量}'";
//获取一条,一维数组
$result=mysqli_query($con,$sql);
$data=mysqli_fetch_array($result,MYSQLI_ASSOC);
//获取多条,二维数组
$result=mysqli_query($con,$sql);
while($res=mysqli_fetch_array($result,MYSQLI_ASSOC)){
$data[]=$res;
}
2、面向对象
$mysqli = new mysqli("localhost", "root", "123456", "表名");
if($mysqli->connect_errno){//连接成功errno应该为0
die('Connect Error:'.$mysqli->connect_error);
}
$mysqli->set_charset('utf8');
增:
$sql="INSERT INTO `表名`(`字段名`,`字段名`) VALUES('{变量}','{变量}')";
$res = $mysqli->query($sql);
//插入成功返回真,插入失败返回假
if($res){
//插入成功则输出自增主键的id和受影响行数
echo "AUTO_INCREMENT:".$mysqli->insert_id; echo "<hr/>"; echo "AFFECTED ROWS:".$mysqli->affected_rows;
}else{
//插入失败则输出错误编号和错误信息
echo "Error:"; echo $mysqli->errno.":".$mysqli->error;
}
删:
$sql="DELETE FROM `表名` WHERE id='{条件变量}'";//删除一定要书写条件,防止误删
$res = $mysqli->query($sql);
//删除成功返回真,删除失败返回假
if($re){
echo '删除成功';
}else{
echo '删除失败';
}
改:
$sql="UPDATE `表名` SET `字段名`='{变量}' WHERE `id`='{条件变量}'";
$res = $mysqli->query($sql);
//插入成功返回真,插入失败返回假
if($res){
echo '修改成功';
}else{
echo '修改失败';
}
查:
$sql="SELECT * FROM `表名` WHERE `id`='{条件变量}'";
//获取一条,一维数组
$mysqli_result = $mysqli->query($sql);//获取查询结果集
$data = $mysqli_result->fetch_array(MYSQLI_ASSOC); //关联数组
//获取多条,二维数组
$mysqli_result = $mysqli->query($query);
while($row = $mysqli_result->fetch_array(MYSQLI_ASSOC)){
$data[] = $row;
};
三、PDO扩展链接数据库:
$mysql_conf = array(
'host' => '127.0.0.1:3306',
'db' => 'test',
'db_user' => 'root',
'db_pwd' => '123456',
);
$pdo = new PDO("mysql:host=" . $mysql_conf['host'] . ";dbname=" . $mysql_conf['db'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);//创建一个pdo对象
$pdo->exec("set names 'utf8'");
$sql = "select * from user where name = '条件变量'";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(1, 'joshua', PDO::PARAM_STR);
$rs = $stmt->execute();
if ($rs) {
// PDO::FETCH_ASSOC 关联数组形式 // PDO::FETCH_NUM 数字索引数组形式
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
var_dump($row);
} }