正在导出 MySQL 到 CSV php

Exporting MySQL to CSV php

最近我一直在开发一个数据库 table 左右的网站。

我正在尝试制作一个按钮来将 MySQL table 下载到 CSV 文件,但我在 CSV 文件 str_replace() expects at least 3 parameters, 2 given in C:\xampp\htdocs\leif\export.php on line 37

中收到此错误

按钮代码:

<form action="export.php" method="post" name="export_excel">

            <div class="control-group">
                <div class="controls">
<button type="submit" id="export" name="export" class="btn btn-primary button-loading" data-loading-text="Laster...">Til CSV</button>
                </div>
            </div>
        </form>

这是创建 CSV 的文件:

<?php


mysql_connect("localhost", "xxxxx", "xxxxx") or die (mysql_error ());

mysql_select_db("dom_oversikt") or die(mysql_error());

//header to give the order to the browser
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=domener.csv');

//select table to export the data
$select_table=mysql_query('select * from server1');
$rows = mysql_fetch_assoc($select_table);

if ($rows)
{
getcsv(array_keys($rows));
}
while($rows)
{
getcsv($rows);
$rows = mysql_fetch_assoc($select_table);
}

// get total number of fields present in the database
function getcsv($no_of_field_names)
{
$separate = '';


// do the action for all field names as field name
foreach ($no_of_field_names as $field_name)
{
if (preg_match('/\r|\n|,|"/', $field_name))
{
$field_name = '' . str_replace('', $field_name) . '';
}
echo $separate . $field_name;

//sepearte with the comma
$separate = ',';
}

//make new row and line
echo "\r\n";
}
?>

你需要在str_replace中输入3个参数!

str_replace("String to search","String to replace with","Source String"); 

http://php.net/manual/en/function.str-replace.php

In addition, please don't use mysql_* function , it's removed now you should use mysqli_* or PDO instead

查看文件中的第 37 行。您缺少第三个参数,即要替换字符的字符串。

str_replace('', $field_name);

PHP str_replace() 函数有3个必填参数,请检查这个

str_replace ($search, $replace, $subject);

参考访问PHP Manual

您在 str_replace 调用中缺少第三个参数。 上瘾时,你必须先捕捉正则表达式的结果,然后使用 preg_match_all 而不是 preg_match 来替换所有出现的地方。

preg_replace你可以在一行代码中全部替换:

$field_name = preg_replace('/(\r|\n|,|")/','', $field_name);