一、php连接数据库与基本操作
(1)php创建数据库连接
语法:Object mysqli_connect(‘域名’,‘DB账号‘,‘DB密码,‘DB库名’
例子:$con=mysqli_connect('localhost','root','','day2db');
(2)想DB中插入数据时包含中文出现乱码的解决方案
语法:mysqli_query($con,'set names utf8');
(3)设置client端和server端保持字符编码一致
语法:mysqli_query($con,'set character_set_client=utf8');
mysqli_query($con,'set character_set_results=utf8');
(4)执行sql语句
语法:$结果=$DB连接->query(sql语句);
例子:var_dump($result=$con->query($sql));
二、php操作数据库【数据库】操作
(1)增加:Insert into 'userinfoList'('userName','password') values([value-1],[value-2]);
(2)查询:Select 'userName','password' from 'userinfoList' where 1;
(3)修改:Update 'userinfoList',set 'userName'=[value-1],'password'=[value-2] where 1;
(4)删除:Delete from 'userinfoList' where 1;
三、使用sql语句基本【模板】
(1)建立连接
$con=mysqli_connect('localhost','root','day2db');
(2)判断是否连接
if($con){};
(3)设置编码
mysqli_query($con,'set names utf8');
mysqli_query($con,'set character_set_client=utf8');
mysqli_query($con,'set character_set_results=utf8');
(4)创建sql语句
$sql='select * from friendslist where 1';
(5)执行sql语句,并获得结果
$result=$con->query($sql);
(6)判断结果条数
if($result->num_rows>0);
(7)拼凑结果
for($i=0,$jsoninfo=[];$row=$result->fetch_assoc();$i++){
$jsoninfo[]=$row;
(8)json化返回
echo json_encode($jsoninfo);
网友评论