运行多行响应查询时,MySQL查询返回0

时间:2017-02-26 19:56:58

标签: php concatenation

当我在PHP中运行以下代码时

$connect = mysqli_connect("localhost", "root", "dbpass", "db");

function csvfromarray($array) {
    $result = $array[0]+","+$array[1];
    return $result;
}

$query = mysqli_query($connect, "SELECT * FROM dbtable");
$row = mysqli_fetch_assoc($query);
$data = array();
$i = 0;

while($row = mysqli_fetch_assoc($query)) {
        $data[$i] = $row['last'];
        $i++;
}
$csv = csvfromarray($data);
echo $csv;
mysqli_close();

当我应该返回“lname1,lname2”时,我最终得到一个回音为“0”的响应。

1 个答案:

答案 0 :(得分:0)

所有chris85的东西都是正确的......

这里有一个整理:

$query=mysqli_query($connect,"SELECT `last` FROM dbtable");
$data=array();
while($row=mysqli_fetch_assoc($query)) {
    $data[]=$row['last'];    // push into array
}
echo implode(',',$data);   // echo comma-separated values
mysqli_close();