Mysql 增删改
<?php //删除 mysql_connect("localhost","root","123"); if(mysql_errno()>0){ exit('数据库连接失败'.mysql_error()); } mysql_set_charset("utf8"); mysql_select_db("stu"); $sql="delete from stu where id=10"; $res=mysql_query($sql); if($res && mysql_affected_rows()){ echo "删除成功, 删除了".mysql_affected_rows()."行"; }else{ echo "删除失败"; } mysql_close();
<?php //查询 mysql_connect("localhost","root","123"); if(mysql_errno()>0){ echo "连接数据库失败".mysql_error; } mysql_set_charset(utf8); mysql_select_db("stu"); $sql="select id,name,sex,age from stu"; $res=mysql_query($sql); if($res)( while ($row) )
<?php $link=mysql_connect('localhost','root','123'); if(!$link){ die('连接失败:'.mysql_error()); } echo mysql_get_client_info(); echo "<br/>"; echo mysql_stat(); echo "<br/>"; echo mysql_client_encoding(); echo "<br/>"; echo mysql_get_host_info(); echo "<br/>"; echo mysql_get_proto_info(); echo "<br/>"; echo mysql_get_server_info();
增
<?php header("content-type:text/html;charset=utf-8"); //1.连接mysql服务器 //mysql_connect 连接 //mysql_pconnect 持久连接 //$link = @mysql_connect("localhost","root","1234") || exit("数据库连接失败!"); //$link = @mysql_connect("localhost","root","1234") || die("数据库连接失败!"); $link = @mysql_connect("localhost","root","123"); //2.判断是否连接成功 //echo mysql_errno(); //echo mysql_error(); if(mysql_errno()){//判断错误号 exit("错误号为:".mysql_errno()."错误信息为:".mysql_error().',数据库连接失败!');//输出错误信息 } //3.设置字符集 mysql_set_charset("utf8");//设置客户端的字符集为utf8 //4.选择数据库 mysql_select_db("lamp69",$link);//use lamp69 //5.准备sql语句 //注册用户 $sql = "insert into bbs_user(username,age,sex) values('zhangsan','20','0')"; //6.发送sql语句 $res = mysql_query($sql);//bool //7.处理结果 if($res && mysql_insert_id()){ echo "注册成功!用户id为:".mysql_insert_id();//获取上一步insert操作的主键id }else{ echo "注册失败!"; } //8.关闭数据库连接 mysql_close();
改
<?php //1.连接服务器 mysql_connect("localhost","root","123"); //2.判断错误 if(mysql_errno()){ exit("数据库连接失败!".mysql_error()); } //3.设置字符集 mysql_set_charset("utf8"); //4.选择数据库 mysql_select_db("lamp69"); //5.准备sql语句 //$sql = "update bbs_user set age=100 where id>=10 && id<=15"; $sql = "update bbs_user set age=101 where id between 10 and 15"; //6.发送sql语句 $res = mysql_query($sql); //var_dump($res); //echo mysql_affected_rows(); //7.处理结果 if($res && mysql_affected_rows()){ echo "修改成功!影响行数为:".mysql_affected_rows();//返回上一条sql语句执行后的影响行数 }else{ echo "没有修改!"; } //8.关闭连接 mysql_close();
删
<?php //1.连接服务器 mysql_connect("localhost","root","123"); //2.判断错误 if(mysql_errno()>0){ exit("数据库连接失败!".mysql_error()); } //3.设置字符集 mysql_set_charset("utf8"); //4.选择数据库 mysql_select_db("lamp69"); //5.准备sql语句 $sql = "delete from bbs_user where id>10"; //6.发送sql语句 $res = mysql_query($sql); //7.处理结果 if($res && mysql_affected_rows()){ echo "删除成功,删除了".mysql_affected_rows()."行"; }else{ echo "删除失败!"; } //8.关闭数据库连接 mysql_close();
查询
<?php //1.连接数据库 mysql_connect("localhost","root","123"); //2.判断错误 if(mysql_errno()){//如果没有出错则返回 0(零) exit("数据库连接失败!".mysql_error()); } //3.设置字符集 mysql_set_charset("utf8"); //4.选择数据库 mysql_select_db("lamp69"); //5.准备sql语句 //$sql = "select id,username,age,sex from bbs_user where id>20"; $sql = "select id,username,age,sex from bbs_user"; //6.发送sql语句 $res = mysql_query($sql); #var_dump($res);//返回结果集资源 #echo mysql_num_rows($res);//获取结果集的行数 //7.处理结果集 if($res && mysql_num_rows($res)>0){ //mysql_fetch_array /*$row = mysql_fetch_array($res,MYSQL_ASSOC); echo "<pre>"; var_dump($row); echo "</pre>"; */ /*while($row = mysql_fetch_array($res,MYSQL_ASSOC)){ echo "<pre>"; var_dump($row); echo "</pre>"; }*/ //mysql_fetch_assoc while($row = mysql_fetch_assoc($res)){ echo "<pre>"; var_dump($row); echo "</pre>"; } //mysql_fetch_object /*while($row = mysql_fetch_object($res)){ echo "<pre>"; var_dump($row); echo "</pre>"; //echo $row->username; }*/ //mysql_fetch_row /*while($row = mysql_fetch_row($res)){ echo "<pre>"; var_dump($row); echo "</pre>"; }*/ } //8.关闭连接 mysql_free_result($res);//释放结果集资源 mysql_close();