我在一个网站上工作,它在 MySQL 中有一个表,其中包含几行的 HTML。每次我尝试从表中提取数据时,它都会为包含 HTML 的行返回“”。其他行(没有 HTML 的行)返回正确的数据。我转义了所有双引号和单引号,整行都在一行上(没有换行符)。一段时间以来,我一直在尝试解决这个问题,但我无法弄清楚。有什么想法吗?
<?php
$con = mysqli_connect('localhost', 'exampleuser', 'examplepass', 'exampledb');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT * FROM Publications;";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_row($result)) {
echo "\n<br>\"" . $row['Publications'] . "\"\n<br>";
}
?>
我放入的 HTML 很长,所以我只粘贴第一部分。请记住,HTML 中没有换行符。我只是把它们放进去,这样更容易阅读。
<p class="MsoTitle"><span style="font-size: 12pt;">Publications</span><span style="font-
size: 12pt; font-weight: normal;"></span></p><p class="MsoNormal"><strong>
<span> </span></strong></p><p class="MsoNormal"
style="margin-left: 0.25in; text-indent: -0.25in;">1. <strong><span style="font-weight:
normal;">C. V. Krishnan and M. Garnett, Electrochemical Behavior of the Super Antioxidant,
α-</span></strong></p><p class="MsoNormal" style="margin-left: 0.25in; text-indent:
-0.25in;"><strong><span style="font-weight:
这是返回的内容:
""
最佳答案
根据你我之间交换的评论。(关闭问题)
Notice: Undefined index: Publications in /home/garnett/public_html/test.php on line 14
这源于您的专栏被命名为 publications
而不是 Publications
。
$row['Publications']
和 $row['publications']
完全是两种不同的动物。
列名区分大小写,这就是您没有得到任何数据输出的原因。
在您的文件顶部使用错误报告,将揭示潜在的错误,如果您在代码中发现任何错误,就像您从建议性评论中看到的那样。
error_reporting(E_ALL);
ini_set('display_errors', 1);
http://php.net/manual/en/function.error-reporting.php
以及
or die(mysqli_error($con))
到mysqli_query()
编辑:
正如 E-Rock 在评论中指出的那样:
“mysqli_fetch_row()
返回行数据的枚举数组。如果您需要关联数组,则需要 mysqli_fetch_assoc()
” .感谢电子摇滚。
关于php - MySQL 返回包含 HTML 的空白行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25373525/