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;
}