如何在页面和csv文件中显示mysql数据库数据?

问题描述:

我已经开发了名为dbdata的html简单表单,并且有一个提交按钮,一旦点击它,就会转到一个名为displaydb.php的页面



i have developed html simple form called dbdata and there is a submit button and once click it, it goes to a page called displaydb.php

<?php

	define('DB_NAME', 'phpdatabase');
	define('DB_USER', 'root');
	define('DB_PASSWORD', 'root');
	define('DB_HOST', 'localhost');

	$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
	
	$filename = 'Uploads/'.strtotime("now").'.csv';
	
	
	$sql = mysqli_query($link,"SELECT * FROM userdb") or die(mysqli_error());
	$num_rows = mysqli_num_rows($sql);
	
	if($num_rows >= 1)
	{		
	$row = mysqli_fetch_assoc($sql);
	$fp = fopen($filename,"w");
	$seperator = "";
	$comma = "";
	
	foreach($row as $name => $value){
	
	$seperator .= $comma . '' .str_replace('','""',$name);
	$comma = ",";
	
	}
	$seperator .= "\n";
	echo $seperator;
	
	fputs($fp,$seperator);
	
	mysqli_data_seek($sql, 0);
	
	while($row = mysqli_fetch_assoc($sql));
	{
	$seperator = "";
	$comma = "";
    
	foreach($row as $name => $value){
	$seperator .= $comma . '' .str_replace('','""',$value);
	$comma = ",";
	}
	
	$seperator .= "\n";

	fputs($fp,$seperator);
	}
	fclose($fp);
	}


?>





但这给了我一个错误叫





but this gives me an error called

引用:

警告:在第41行的C:\ wamp64 \ www \ Websites \displayDB.php中为foreach()提供的参数无效

Warning: Invalid argument supplied for foreach() in C:\wamp64\www\Websites\displayDB.php on line 41





这是第二个foreach循环。





which is second foreach loop.

foreach($row as $name => $value){
	$seperator .= $comma . '' .str_replace('','""',$value);
	$comma = ",";
	}





当我尝试使用一个foreach循环时。它将在不同的页面上打印数据库数据。但只有数据库标题。



id,姓名,地址

就是这样。

它会创建一个csv文件以及数据库标题。

但我希望所有数据都打印在文件上。

这就是为什么我创建了第二个foreach循环。



我尝试了什么:



我添加第二个foreach循环代码之前,我编译好了,编译得很好。

现在它无法工作。



i认为第二个foreach循环有错误。



when i try this with one foreach loop. it will print database data on a differant page. but only database header only.

id, name, address
like that.
and it will creates a csv file as well with database header.
but i want all data to be printed on the file.
that's why i have created a second foreach loop.

What I have tried:

before i add this second foreach loop code, i have compiled and it compiled well.
now its not working.

i think there is an error on second foreach loop.

link = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);


filename = ' 上传/' .strtotime( now)。' 。csv';


filename = 'Uploads/'.strtotime("now").'.csv';


sql = mysqli_query(
sql = mysqli_query(