PHP操作数据库增删改查 安卓+php控制数据库 安卓连接数据库

一、前端

        前端以安卓为例子,使用HttpURLConnection 进行http请求运行服务器中的php代码,获得返回值。HttpURLConnection 进行http请求的具体使用方法如下:安卓HttpURLConnection 进行http请求(传递数据 获取数据 主线程禁止网络请求)以get方式为例_m0_49558200的博客-CSDN博客_安卓connectionicon-default.png?t=M1L8https://blog.csdn.net/m0_49558200/article/details/122387710?spm=1001.2014.3001.5501

        其余前端的方式只要可以进行http请求均可。

        安卓中示例代码如下

Button btn3=(Button)findViewById(R.id.button3);//将此事件封装进按钮中
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){//网络请求需要建立新线程
                    @Override
                    public void run()
                    {
                        try {
                            String sql="SELECT  id " +
                                    "FROM name";//此处为sql语句
                            sql= Uri.encode(sql);//编码
                            String path="http://xxx.xxx.xxx.xxx/db.php?sql="+sql;
                            URL url = new URL(path);
                            //2. HttpURLConnection
                            HttpURLConnection conn=(HttpURLConnection)url.openConnection();

                            //3. set(GET)
                            conn.setRequestMethod("GET");
                            conn.setConnectTimeout(6000);
                            conn.setReadTimeout(6000);//响应时间



                            //获取返回值
                            StringBuffer buffer = new StringBuffer();
                            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
                            String line = "";
                            while ((line = reader.readLine()) != null) {
                                buffer.append(line);
                            }
                            reader.close();
                            Log.i("res",buffer.toString());//查询结果

                            

                        }
                        catch(MalformedURLException e){
                            e.printStackTrace();
                        }
                        catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();

            }
        });

二、后端(服务器)部分

connec.php文件如下,用来连接数据库

<?php
header("Content-type: text/html;charset=utf8");

$host='xxx.xxx.xxx.xxx'//主机的ip地址(内网)
$user='xxxxxx'//数据库的用户名 初始应该都是root
$password='xxxxx'//数据库密码
$dbName='xxxxx'//数据库名
$con=new mysqli($host,$user,$password,$dbName);
con->query("SET NAMES utf8");
?>

接下来是正式进行数据库增删改查的php文件,文件名为db.php

<?php
header("Content-type: text/html;charset=utf8");
include 'connect.php'
//引用刚才的文件,连接数据库
$sql=$_GET["sql"];
//此处我的逻辑是通过get方式获取到sql语句。
//如果想要将sql语句写死在php中,可以直接
//$sql='xxxxxxxx'//此处为sql语句

if($con->$connect_error){
die("error");
}
else{
$sql=urldecode($sql);//我的sql语句由前端传递到后端,进行了编解码操作
//不需要解码可以不用上面一句话

$res=$con->query($sql);
print_r($res->fetch_all(RDO::FETCH_ASSOC));
//返回值是一个数组,故不可以用echo返回
}

?>

示例的mysql语句:

select id from name

数据表name

查询结果

I/sql langaues: Array(    [0] => Array        (            [0] => 3        )    [1] => Array        (            [0] => 2500        )    [2] => Array        (            [0] => 4554        )    [3] => Array        (            [0] => 45514        )    [4] => Array        (            [0] => 422554        ))

注意:1.get方式传递的sql语句需要进行编码后再传递。

           2.php语言当返回值为一个数组时,不可以使用echo,要用print_r。否则返回值只有一个“Array”字符串。

           3.要先在服务器中安装MYSQL数据库与PHP功能。

           4.在微信小程序中需要为服务器购买域名再使用wx.request进行http请求。  

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值