@屏蔽错误信息,error_reporting(0),也是屏蔽错误信息
连接数据库时,如有有错就报错误信息
$m=@new mysqli('localhost','rootr','','db');$m->set_charset('utf8');if($m->connect_error){echo $m->connect_error;exit;}$m->close();//关闭数据库连接
连接数据库
$conn = mysql_connect('localhost','root','');
设置连接数据库的编码字符集为utf8
mysql_query('set names utf8',$conn);
选择数据库
mysql_select_db('db',$conn);
执行创建数sql据库语句
创建数据库
mysql_query('create database db',$conn);
删除数据库
mysql_query('drop database if exists db',$conn);
插入一条信息
$t = time();
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "insert into guest values(null,'张三','公司位置?','公司在哪里,电话是多少',$t,'$ip')";
给数据库发送一条执行指令
mysql_query($sql,$conn);
关闭数据库连接
mysql_close($conn);
删除数据
$m->query('delete from stu where id in(1,2,3)');
$m->close();
预处理语句删除数据
$stmt=$m->prepare('delete from stu where id in(?,?)');
$id1=12;
$id2=4;
$stmt->bind_param('ii',$id1,$id2);/ii表示2个整数
$stmt->execute();
$stmt->close();
修改数据
$m->query("update stu set name=='李四'where id=2");
预处理语句修改数据
$stmt=$m->prepare('update stu set name=? where id =?');
$stmt->bind_param('si',$name,$id);
$name='张三';
$id=11;
$stmt->execute();
使用预处理语句查询
$stmt=$m->prepare('slect * from sta');
$stmt->execute;
$result=$stmt->get_result();
while($rs=$result->fetch_row(){
print_r($rs);
}
$stmt->free_result();
mysql_list_dbs();列出数据库
mysql_get_client_info()取得mysql的客户端信息
mysql_get_host_info()取得主机信息
mysql_get_proto_info()取得协议信息
mysql_get_server_info()取得服务器信息
mysql_result($result,2,1)表示查询的第二行第1列位置的数据显示结果为赵六
查询数据库里面的信息有几条
$conn=@mysql_connect('localhost','root','');mysql_query('set names utf8',$conn);mysql_select_db('db',$conn);$result=mysql_query('select count(*) from guest',$conn);echo mysql_result($result,0);mysql_close($conn);
1
查询显示姓名时间
$conn=@mysql_connect('localhost','root','');mysql_query('set names utf8',$conn);mysql_select_db('db',$conn);$result=mysql_query('select gname,gtime from guest');if($rs=mysql_fetch_row($result)){echo '姓名:'.$rs[0].'时间:'.date('Y-m-d H:i:s',$rs[1]).'<br>';}mysql_close($conn);

$conn=@mysql_connect('localhost','root','');mysql_query('set names utf8',$conn);mysql_select_db('db',$conn);$result=mysql_query('select gname,gtime from guest');echo '<pre>';if($rs=mysql_fetch_row($result)){print_r($rs);}mysql_close($conn);

mysql_fetch_assoc($result)显示关联下标名称
$conn=@mysql_connect('localhost','root','');mysql_query('set names utf8',$conn);mysql_select_db('db',$conn);$result=mysql_query('select gname,gtime from guest');echo '<pre>';if($rs=mysql_fetch_assoc($result)){print_r($rs);}mysql_close($conn);

mysql_fetch_array($result)打印输出关联下标及名称,打印两遍浪费内存
$conn=@mysql_connect('localhost','root','');mysql_query('set names utf8',$conn);mysql_select_db('db',$conn);$result=mysql_query('select gname,gtime from guest');echo '<pre>';if($rs=mysql_fetch_array($result)){print_r($rs);}mysql_close($conn);

mysql_free_result($result);释放内存
=unset($result);
echo mysql_num_fields($result);查询有多少列信息
echo mysql_affected_rows();显示上一次的mysql操作影响了几行可以用来判断是否执行正确
mysqli创建数据库删除数据库
$conn=mysqli_connect('localhost','root','','db',3306);//创建数据库//mysqli_query($conn,'create database da2');//如果存在删除数据库$stmt=mysqli_prepare($conn,'drop database if exists da2');mysqli_execute($stmt);mysqli_close($conn);
使用预处理语句,查询数据库信息
$m=new mysqli('localhost','root','','db');$m->set_charset('utf8');$r=$m->query('select user from mysql.user');while($rs=$r->fetch_row()){print_r($rs);}$r->free();//释放查询结果$r所占用的内存$m->close();//关闭数据库连接
$m->select_db('mysql');//切换当前连接指定的数据库
$r=$m->query('select user from user');//从数据库中查询信息
if($rs=$r->fetch_row()){
print_r($rs);
}
mysql 释放内存
mysql_free_result($result);
使用预处理语句实现数据的插入
$m=new mysqli('localhost','root','','db');$m->set_charset('utf8');//使用预处理语句,实现数据插入$n='张三丰';$stmt=$m->prepare('insert into stu values(null,?)');$stmt->bind_param('s',$n);$stmt->execute();$stmt->free_result();//释放查询结果$r所占用的内存$m->close();//关闭数据库连接
预处理查询语句,一定要用while语句,使用if只会显示一条信息
$m=new mysqli('localhost','root','','db');$m->set_charset('utf8');$result=$m->query('select * from stu');echo '<pre>';while($rs=$result->fetch_row()){print_r($rs);}$m->close();//关闭数据库连接
删除数据
$m=new mysqli('localhost','root','','db');$m->set_charset('utf8');$m->query('delete from stu where sid in(1,2)');$m->close();//关闭数据库连接
使用预处理删除数据
bind_param('ii',$id1,$id2);
ii表示后面跟了2个变量,也可用s,d
$m=new mysqli('localhost','root','','db');$m->set_charset('utf8');$stmt=$m->prepare('delete from stu where sid in (?,?)');$id1=3;$id2=4;$stmt->bind_param('ii',$id1,$id2);$stmt->execute();$m->close();//关闭数据库连接
修改数据
$host = 'localhost';$user = 'root';$pass = '';$dbname = 'db';$charset = 'utf8';$m = new mysqli($host,$user,$pass,$dbname);$m->set_charset($charset);$m->query("update stu set sname='李四' where sid=7");
用预处理语句修改数据信息
<?php$host = 'localhost';$user = 'root';$pass = '';$dbname = 'db';$charset = 'utf8';$m = new mysqli($host,$user,$pass,$dbname);$m->set_charset($charset);$stmt=$m->prepare('update stu set sname=? where sid=?');$stmt->bind_param('ss',$name,$id);$name='赵六六';$id=7;$stmt->execute();
<?php$m=new mysqli('localhost','root','','db');if($m->connect_error){exit('数据库连接失败请检查....');}if($stmt=$m->prepare('select count(*) from member where maccount=?')){$stmt->bind_param('s',$account);$account='222';$stmt->execute();$stmt->bind_result($count);$stmt->fetch();$stmt->free_result();$stmt->close();echo '结果是:'.$count;}$m->close();//返回结果1表明有这个用户名
<?php$m=new mysqli('localhost','root','','db');$m->set_charset('utf8');//指定字符集if($m->connect_error){exit('数据库连接失败请检查....');}if($stmt=$m->prepare('select * from member')){$stmt->execute();$result=$stmt->get_result();$rows=$result->fetch_all(MYSQLI_NUM);echo '<pre>';print_r($rows);}

fetch_all(MYSQLI_NUM)=2
fetch_all(MYSQLI_ASSOC)=1
fetch_all(MYSQLI_BOTH)=3
echo MYSQLI_NUM,MYSQLI_ASSOC,MYSQL_BOTH;

浙公网安备 33010602011771号