数据库与PHP交互基础

一、数据库查询语句

      描述:去数据库的指定表中根据条件查询语句

$con=mysqli_connect('localhost','root','','librarydb');

       mysqli_query($con,'set names utf8');

       mysqli_query($con,'set character_set_client=utf8');

       mysqli_query($con,'set character_set_results=utf8');

       $sql="select * from book where bookId='1' ";

       $result=$con->query($sql);

       if($result->num_rows>0){

               $info=[];

               for($i=0;$row=$result->fetch_assoc();$i++){

                        $info[$i]=$row;

       }

       print_r($info);

}

二、数据库修改语句

 $con=mysqli_connect('localhost','root','','librarydb');

mysqli_query($con,'set names utf8');

mysqli_query($con,'set character_set_client=utf8');

mysqli_query($con,'set character_set_results=utf8');
$sql="update book set bookName='鲁滨逊漂流记' where bookId='123' ";
$result=$con->query($sql);
if($result){
    	echo '修改成功';
    }else{
    	echo '修改失败,请重新修改';
    }

三、数据库增加语句

$con=mysqli_connect('localhost','root','','librarydb');
if($con){
	echo '连接成功Loading...';

    mysqli_query($con,'set names utf8');
    mysqli_query($con,'set character_set_client=utf8');
    mysqli_query($con,'set character_set_results=utf8');

    $sql="insert into book (bookId,bookName,bookAuthor,bookPrice,bookNum) values('3','时间科学','不知道','85','16')";

    $result=$con->query($sql);

    if($result){
    	echo '添加成功';
    }else{
    	echo '添加失败,请重新添加';
    }

}else{
	echo '连接失败,请重连';
}

四、数据库删除语句

$con=mysqli_connect('localhost','root','','librarydb');
if($con){
	echo '连接成功Loading...';

    mysqli_query($con,'set names utf8');
    mysqli_query($con,'set character_set_client=utf8');
    mysqli_query($con,'set character_set_results=utf8');

    $sql="delete from book where bookName='时间科学'";

    $result=$con->query($sql);

    if($result){
    	echo '删除成功';
    }else{
    	echo '删除失败,请重新删除';
    }

}else{
	echo '连接失败,请重连';
}

 五、html,数据库,php前后端交互

      1、引入jquery框架

      2、需要和后台交互的话,调用 ajax 方法

            ajax方法需要一系列参数:type : 'post/get',

                                                     url : '给一个目标网址(一定要加http)',

                                                     datatype : 'json',

                                                     data : (data是post专有的){  ...  },

                                                     success : function(data){

                                                              console.log(data);

               }

在php中内置了两个对象用来接收前端发来的消息

    $GET:用于获取在前端通过get请求发来的信息

    $SET:用于获取在前端通过post请求发来的信息

      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值