MySQLI命令后发生的PHP Echo语句

时间:2016-04-25 17:30:37

标签: php output echo

我有一个用于清理一些MySQL数据表(截断命令)的php,并输出一些消息,表明任务已完成(ECHO语句)。

然后,PHP通过LOAD DATA INFILE命令将几个CSV文件加载到最近清理的数据库表中。接下来是几个echo语句,表明任务已完成。

我遇到的问题是,只有在MYSQL工作完成后才能输出到屏幕。我希望在MYSQL进程之前使用echo语句,这可能需要一分钟或更长时间,以便用户知道该进程正在进行并且可能需要一些时间。

echo "<center><strong>Cleaning up tables in preparation of importing data files...<strong></center><br>";

//select the database we are going to be using
mysqli_select_db($conn, "database");

//truncate (empty) the three tables
mysqli_query($conn, 'TRUNCATE TABLE firsttable;');
mysqli_query($conn, 'TRUNCATE TABLE secondtable;');
mysqli_query($conn, 'TRUNCATE TABLE thirdtable;');

//provide feedback on process
echo "<center><strong>Tables cleaned!</strong></center><br>";

//queries which will load datafiles into the tables
$query = "LOAD DATA INFILE \"D:/wamp64/www/folder/firsttable.csv\" INTO TABLE altoccurrences COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES";
$query2 = "LOAD DATA INFILE \"D:/wamp64/www/folder/secondtable.csv\" INTO TABLE altimages COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES";
$query3 = "LOAD DATA INFILE \"D:/wamp64/www/folder/thirdtable.csv\" INTO TABLE altidentifications COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES";

//send queries to MySQL and check for potential errors.
$result = mysqli_query($conn, $query) or die ('Could not connect to mysqli: ' . mysqli_error($conn));
$result2 = mysqli_query($conn, $query2)  or die ('Could not connect to mysqli: ' . mysqli_error($conn));
$result3 = mysqli_query($conn, $query3)  or die ('Could not connect to mysqli: ' . mysqli_error($conn));

//provide feedback on process
echo "<center><h2><strong>Your Files Have Been Inserted into Database!</strong></h2></center><br>";

1 个答案:

答案 0 :(得分:1)

PHP通常会在发送内容之前缓冲内容。在每个echo之后尝试以下代码,尝试强制PHP立即发送数据。

@flush();