删除出现的“、、”从 MySQL 数据库输出 PHP

发布于 2024-10-20 14:34:49 字数 986 浏览 4 评论 0原文

我使用以下代码将 MySQL 数据库的内容输出到文本文件中:

<?php
$host = 'localhost';
$user = 'myusername';
$pass = 'mypassword';
$db = 'mydbname';
$table = 'mytablename';
$file = 'outputfilename';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
//$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "\n";

$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].",";
}
$csv_output .= "\n";
}
//echo $csv_output;
$myFile = "output.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $csv_output;
fwrite($fh, $stringData);
fclose($fh);
exit;
?>

但是我想知道如何用以下代码替换出现的字符串 ",,,""," 在进入文本文件之前。

I am outputting the contents of my MySQL Database into a text file using the below code:

<?php
$host = 'localhost';
$user = 'myusername';
$pass = 'mypassword';
$db = 'mydbname';
$table = 'mytablename';
$file = 'outputfilename';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
//$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "\n";

$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].",";
}
$csv_output .= "\n";
}
//echo $csv_output;
$myFile = "output.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $csv_output;
fwrite($fh, $stringData);
fclose($fh);
exit;
?>

However I would like to know how I could replace the occurances of the string ",,," with this: "," before it goes into the text file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

飘过的浮云 2024-10-27 14:34:49

不需要 preg_replace()

一个简单的 str_replace 就足够了。

$stringData = str_replace(',,,', ',', $csv_output);

No need for preg_replace().

A simple str_replace will suffice.

$stringData = str_replace(',,,', ',', $csv_output);
可遇━不可求 2024-10-27 14:34:49

问题在于:

$values = mysql_query("SELECT * FROM ".$table."");

如果您得到 ,,,, 这意味着某些列没有值。检测这些列,不要将它们包含在 SELECT 语句中。

如果您自动(preg_replace、str_replace)将多个逗号更改为一个逗号,那么您就可以很好地损坏 CSV 文件。

problem is in:

$values = mysql_query("SELECT * FROM ".$table."");

if you got ,,,, it means that some columns has no value. Detect those columns, do not include them in SELECT statement.

if you will automatically (preg_replace, str_replace) change multiple commas to one comma, you are on good way to corrupt CSV file.

冬天的雪花 2024-10-27 14:34:49

使用 preg_replace()。像这样的东西(未经测试):

$pattern = '/\,\,\,/';
$replacement = ',';
echo preg_replace($pattern, $replacement, $stringData);

Use preg_replace(). Something like this (untested):

$pattern = '/\,\,\,/';
$replacement = ',';
echo preg_replace($pattern, $replacement, $stringData);
小镇女孩 2024-10-27 14:34:49

添加此代码:

while ($csv_output=str_replace(',,', ',', $csv_output)) echo '.';

或更改 for 代码:

for ( $j=0; $j<$i; $j++) {
    $csv_output .= (isset($rowr[$j])) ? $rowr[$j]."," : '';
}

add this code:

while ($csv_output=str_replace(',,', ',', $csv_output)) echo '.';

OR change the for code:

for ( $j=0; $j<$i; $j++) {
    $csv_output .= (isset($rowr[$j])) ? $rowr[$j]."," : '';
}
蹲在坟头点根烟 2024-10-27 14:34:49

str_replace()

$stringData = str_replace(',,,', ',', $stringData);

http://php.net/str-replace

str_replace()

$stringData = str_replace(',,,', ',', $stringData);

http://php.net/str-replace

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文