好的,所以我有一个数据库并正在填充一个表。我通过 php 页面使用 $sqlCommand 连接并填充没有问题。
我的问题是我的字段之一有内容 block /段落。我需要显示段落,但 nl2br() 不起作用。
这是我填充 (site.com/xtblcreate.php) 的方式:
// Create table1 in db x for storing words
$sqlCommand = "CREATE TABLE table1 (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
a TEXT,
b TEXT,
c TEXT,
d TEXT,
page_views INT NOT NULL default '0',
FULLTEXT (a,b,c,d)
) ENGINE=MyISAM";
$query = mysql_query($sqlCommand) or die(mysql_error());
echo "<h3>Success creating table1 in db x</h3>";
// Insert dummy data into the table1 in db x
$sqlCommand = "INSERT INTO table1 (a,b,c,d) VALUES
('something', 'something else', 'a lot \n of info', 'quick conclusion' )";
我需要的是将“很多信息”分成两段。
然后在自己的php页面(site.com/x.php)上查询:
$search_output .= "$count result(s) for <strong>$searchquery</strong><br />";
while($row = mysql_fetch_array($query)){
$id = $row["id"];
$a = $row["a"];
$b = $row["b"];
$c = $row["c"];
$d = $row["d"];
$search_output .= "*<br>$a- <br/><b>c: </b>$c<br /> <br /> b: $b<br />finally, d: $d<br/>";
echo nl2br($c);
//output $c with paragraphs
请对我放轻松,因为我是新手。
最佳答案
在 PHP 中使用单引号时,不会处理诸如 \n
之类的转义序列。
如果您将查询更改为:
$sqlCommand = "INSERT INTO table1 (a,b,c,d) VALUES
('something', 'something else', \"a lot \n of info\", 'quick conclusion' )";
然后我怀疑 nl2br()
会产生您想要的输出。
关于php - 我如何在mysql中显示段落?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22737133/