php连接数据库 搜索数据形成数组,转为字符串
<?php //连接数据库
$db = new mysqli('localhost','root','root','z_0222');
!mysqli_connect_error() or die('数据库连接失败');
$db->query('set names utf8');
//搜索语句
$sql = "select * from student";
$res = $db->query($sql);
//转为数组
$arr = $res->fetch_all();
//数组转为字符串
$str = '';
foreach ($arr as $v){
$str .= implode(',',$v).'^'; //以'^'分隔
}
$str = substr($str,0,strlen($str)-1); //删除最后逗号
echo $str;