我在从MySQL数据库中检索信息时遇到问题。你能救我吗?这是我写的代码:
<?php
$result = mysql_query("SELECT * FROM notas LIMIT 1 ORDER BY id DESC");
while($nt = mysql_fetch_array($result)) {
?>
<div class="nuevos_1">
<div class="over_descripcion">
<div class="over_title">
<h3><a href="/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>"><?php echo $nt[titulo] ?></a></h3>
</div>
<div class="over_autor">
<span><b>Por: </b><a href="/autor/<?php print $nt[autor]; ?>"><?php print ucwords(str_replace("-", " ", $nt[autor])); ?></a> <?php echo $nt[fecha] ?></span>
</div>
<div class="over_texto">
<span><b><?php echo strtoupper(str_replace("-", " ", $nt[categoria])) ?></b> <a href="/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>"><?php echo substr(strip_tags($nt[texto]), 0, 240)."..." ?></a></span>
</div>
<div class="over_ver_mas">
<input type="button" value="Leer más" onclick="location.href='/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>'" />
</div>
</div>
<a href="/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>">
<img src="http://<?php echo $nt[imagen] ?>" border="0" />
</a>
</div>
<?php }; ?>
SELECT notas数据库中没有显示任何内容。它简直消失了......
谢谢!
答案 0 :(得分:6)
限制1必须在ORDER BY之后
答案 1 :(得分:1)
将您的查询更改为
SELECT * FROM notas ORDER BY id DESC LIMIT 1
答案 2 :(得分:0)
错误消息说什么
$result = mysql_query("SELECT * FROM notas LIMIT 1 ORDER BY id DESC");
if (mysql_error()) {
die(mysql_error());
}
答案 3 :(得分:0)
上面的其他答案都是正确的 - 您的查询中出现语法错误。如果您已完成基本步骤:
$result = mysql_query(..) or die(mysql_error());
您已经看到了错误消息。永远不要假设查询成功。始终检查错误情况。
同样,你的输出中有一个小的语法错误:
... <a href="/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>"> ...
^^^^^
您的数组键未引用,这意味着它们被解释为define()
'd常量。 PHP是礼貌的,并且如果实际上不具有该名称的常量,则将这些常量视为字符串,但它将针对每个出现发出警告。它应该是:
<?php echo $nt['fecha'] ... ?>
答案 4 :(得分:-1)
我对PHP不太了解,但是你的select语句仅限于一个结果。您可能想要更改此行:
$result = mysql_query("SELECT * FROM notas LIMIT 1 ORDER BY id DESC");
你不会得到多于一个结果。