从 MySQL 数据库错误中选择最新条目

发布于 2024-09-26 14:20:42 字数 625 浏览 2 评论 0原文

<?php
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("jmvarela_jacket") or die(mysql_error());

$query = 'SELECT * FROM `quote` ORDER BY `id` DESC LIMIT 1'; 

$row = mysql_fetch_array( $query );
echo $row['frase'];
?>

我无法让它发挥作用。

我收到此错误:

警告:mysql_fetch_array():已提供 参数不是有效的 MySQL 结果 资源在 /home/jmvarela/public_html/ihateyourjacket.com/latest/index.php 第 7 行

我尝试选择 mysql 数据库的最新条目。

该表名为“quote”,

共有三个字段:id、frase 和 name。

只是为了澄清(因为这可能是非常糟糕的编码)我试图获取“最大”id 并显示它对应的“frase”。

<?php
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("jmvarela_jacket") or die(mysql_error());

$query = 'SELECT * FROM `quote` ORDER BY `id` DESC LIMIT 1'; 

$row = mysql_fetch_array( $query );
echo $row['frase'];
?>

I cant get this to work.

I get this error:

Warning: mysql_fetch_array(): supplied
argument is not a valid MySQL result
resource in
/home/jmvarela/public_html/ihateyourjacket.com/latest/index.php
on line 7

I am trying to select the latest entry to the mysql database.

The table is called "quote"

There are three fields: id, frase and name.

Just to clarify (because this could be VERY bad coding) I am trying to get the "biggest" id and to display it's correspondant "frase".

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

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

发布评论

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

评论(3

倾城°AllureLove 2024-10-03 14:20:42

您还没有执行您的查询

$结果 = mysql_query($query);

$row = mysql_fetch_array( $result );

试试这个

you have not perform your query

$result = mysql_query($query);

$row = mysql_fetch_array( $result );

try this

岁月打碎记忆 2024-10-03 14:20:42

您似乎没有运行查询。

// construct the query.
$query = 'SELECT * FROM `quote` ORDER BY `id` DESC LIMIT 1'; 

// run the query..THIS IS MISSING.
$result = mysql_query($query);

另外,最好将 SELECT * 更改为 SELECT frase,因为您只对 frase 列感兴趣。这不会将所有不需要的列从 MySql 带到 PHP,从而使您的程序性能更好。

Looks like you are not running the query.

// construct the query.
$query = 'SELECT * FROM `quote` ORDER BY `id` DESC LIMIT 1'; 

// run the query..THIS IS MISSING.
$result = mysql_query($query);

Also it's better to change SELECT * to SELECT frase, since you're interested only in the frase column. This will not bring all the unwanted columns from MySql to PHP, making your program perform better.

超可爱的懒熊 2024-10-03 14:20:42

我不确定是否应该这样做,但我会留下完整的运行代码以供将来参考。

<?php
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("jmvarela_jacket") or die(mysql_error());

// construct the query.
$query = 'SELECT * FROM `quote` ORDER BY `id` DESC LIMIT 1'; 

$result = mysql_query($query);

$row = mysql_fetch_array( $result );
echo $row['frase'];
?>

谢谢大家!

I´m not sure if this should be done but ill leave the complete running code for future refence.

<?php
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("jmvarela_jacket") or die(mysql_error());

// construct the query.
$query = 'SELECT * FROM `quote` ORDER BY `id` DESC LIMIT 1'; 

$result = mysql_query($query);

$row = mysql_fetch_array( $result );
echo $row['frase'];
?>

Thanks to everyone!

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