从查询中仅获取1个结果 - 将结果保存在CSV文件中

I have been dealing with this all day ... I am doing a SQL Query and it suppose to return several rows, but I am only getting one, I would believe it is the first one, I want to save these results/rows in a CSV file.

So when I do the var_dump only one result is showing and I can only write that first line in the CSV file.

session_start();

$file = 'file.csv';

if(isset($_POST['button1'])){

echo("You clicked button one!");

$con=mysqli_connect("localhost","root","","test");

$query = "SELECT * FROM test.tec_milenio_cubo";
$result = mysqli_query($con, $query); // EJECUTAS LA CONSULTA
$row = mysqli_fetch_assoc($result);


var_dump ($row);


$_SESSION['file'] = $file;

$fp = fopen($file, 'w');

foreach ($row as $value) {

    fputcsv($fp, $value);

}

fclose($fp);
mysqli_close($con);

Use a while loop on your $row.

$fp = fopen($file, 'w');

while ($row = mysql_fetch_array($result)) {
    var_dump ($row);
    fputcsv($fp, $row);
}

fclose($fp);

Quixrick is correct.

Your initial code is only pulling one record from the database at a time.

The while loop will continue until all records are pulled and end when $row is not assigned a value.

Happy Coding :-)!