views:

51

answers:

3
<?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".

Thanks!!

+3  A: 

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.

codaddict
Trying it out...
Trufa
@codaddict sorry cant see how the code should end up! Should i still use $row = mysql_fetch_array( $query );echo $row['frase'];
Trufa
@Trufa: Yes. End it as you were ending it before.
codaddict
@codaddict thanks I got it to work with Bhanu´s answer but thanks for the tips! very clarifying!
Trufa
+2  A: 

you have not perform your query

$result = mysql_query($query);

$row = mysql_fetch_array( $result );

try this

Bhanu
Thank you! got it!
Trufa
+2  A: 

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!

Trufa
Clear question with code sample? Check. Response to comment asking for clarification? Check. Upvoted/Accepted answer(s) that were helpful? Check. Complete code listing of final solution? Check. I wish everyone would cover all the bases so thoroughly! Welcome to Stack Overflow. :)
Bill the Lizard
@Bill the Lizard. Thank you very much. It´s been a great ride so far and I´m sure it´s just starting. Cheers!
Trufa