时间:2021-07-01 10:21:17 帮助过:3人阅读
$m->set_charset(‘utf8‘);if($m->connect_error){ echo $m->connect_error; exit;}$m->close();//关闭数据库连接
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);
$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);

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

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

使用预处理语句,查询数据库信息
$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->select_db(‘mysql‘);//切换当前连接指定的数据库$r=$m->query(‘select user from user‘);//从数据库中查询信息if($rs=$r->fetch_row()){ print_r($rs);
$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();//关闭数据库连接
预处理查询语句,一定要用while语句,使用if只会显示一条信息
$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();//关闭数据库连接
删除数据
$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();//关闭数据库连接
使用预处理删除数据bind_param(‘ii‘,$id1,$id2);ii表示后面跟了2个变量,也可用s,d
$m=new mysqli(‘localhost‘,‘root‘,‘‘,‘db‘);$m->set_charset(‘utf8‘);$m->query(‘delete from stu where sid in(1,2)‘);$m->close();//关闭数据库连接
修改数据
$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);}

php 连接mysql
标签: