php 连接mysql
$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‘,‘‘);
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=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=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();//关闭数据库连接
$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);
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。