开发项目中学习PHP-多数据源配置

 

PHP框架为TP5:

public function selectInfos(){ 
        $id = 1;
       $mysql_conf = array(
           // 数据库类
           'type'        => 'mysql',
           // 服务器地址
           'hostname'    => 'xxxx:3306',
           // 数据库名
           'database'    => 'xxx',
           // 数据库用户名
           'username'    => 'xxx',
           // 数据库密码
           'password'    => 'xxx',
           // 数据库连接端口
           'hostport'    => '3306');

       $mysqli =  @new mysqli($mysql_conf['hostname'], $mysql_conf['username'], $mysql_conf['password']);
       //$resut = $dbc->query('select * from mytable');
       if ($mysqli->connect_errno) {
           die("could not connect to the database:\n" . $mysqli->connect_error);//诊断连接错误
       }
       $mysqli->query("set names 'utf8';");//编码转化
       $select_db = $mysqli->select_db($mysql_conf['database']);
       if (!$select_db) {
           die("could not connect to the db:\n" .  $mysqli->error);
       }

      $sql = "select * from tt where id = $id"; //定义sql
       $res = $mysqli->query($sql);  //执行语句
       if (!$res) {
           die("sql error:\n" . $mysqli->error);
       }
       $infos =  array();
       $infos[0] = [      
           'key1'=>'1',
           'ley2' => '2',
           'key3' => '3'];   //定义数组

     $i=1;

     while ($row = $res->fetch_assoc()) {  //解析结果集
           $infos[$i]=$row;
           $i++;
       }

       $res->free();
       $mysqli->close();
       return $infos;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值