Jump to content

Echo mysql row without repeating and in descending order


xerox02

Recommended Posts

<?php
$query = "SELECT DISTINCT id FROM quotes ORDER BY DESC LIMIT 1"; 

$result = mysql_query($query) or die(mysql_error());


$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['id']. " - ". $row['quotes'];
echo "<br />";
echo $row['id']. " - ". $row['quotes'];
?>

 

I am trying to echo id and quotes column in my QUOTES table without it repeating, and also having it in descending order.

How can I do this?

SELECT DISTINCT id, quotes FROM quotes ORDER BY DESC LIMIT 1

??

 

thanks everyone. I did that, and I got this

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC LIMIT 1' at line 1"

Ah yes. You need to put a column you want to order by there.For example

 

SELECT DISTINCT id, quotes FROM quotes ORDER BY id DESC LIMIT 1

 

Although I don't really see the point of this query...

 

when I echo it comes out

 

7 - My lizard

7 - My lizard

 

Instead of 2 different entries that are in descending order.

while($row = mysql_fetch_array($result)) {
  echo $row['id']. " - ". $row['quotes'];
  echo "<br />";
}

 

I am so sorry, let me explain what I want in detail. I basically want each column in one row to be put into a variable. Then I want to echo out the variable so that each time I print it will be in descending order, and won't repeat. It doesn't have to be put into a variable maybe it can fetched through an array or etc. Whatever is most convenient to echo out the data.

Well? The code above should display all rows fetched by this query.

 

I am trying to put each column on different places on my web page, and this won't allow me to do that because it fetches the ENTIRE row, and not just one column in the row. After that, each time I echo the variable I don't want to repeat and i want to be in descending order. Thanks for the help.

<?php
$query = "SELECT DISTINCT id, quotes FROM quotes ORDER BY id  DESC LIMIT 1"; 

$result = mysql_query($query) or die(mysql_error());


$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['id']. " - ". $row['quotes'];
echo "<br />";
echo $row['id']. " - ". $row['quotes'];

?>

 

When it echos it should be

7 - My Lizard

6 - My Cat

 

but when I print it, it prints

 

7 - My Lizard

7 - My Lizard

 

 

I'm pretty sure it's explained in the manual for mysql_query together with example.

 

<?php
$query = "SELECT DISTINCT id, quotes FROM quotes ORDER BY id  DESC"; 

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)) {
  echo $row['id']. " - ". $row['quotes'];
  echo "<br />";
}

?>

I'm pretty sure it's explained in the manual for mysql_query together with example.

 

<?php
$query = "SELECT DISTINCT id, quotes FROM quotes ORDER BY id  DESC"; 

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)) {
  echo $row['id']. " - ". $row['quotes'];
  echo "<br />";
}

?>

 

This works fine, but I am trying to get only 1 row from my table, and the example code echos the entire table. If I limit it to 1, and echo the same variable twice, it will echo the same thing

 

7 - my lizard

7 - my lizard

 

On my site I want to put each row of my table on different places of the site, but if my entire table echos I can't really specifically put each row where I want it. The only way this works is by making a new variable, but my table is going to get large so making a 100 variables is inefficient, and I am trying to figure out if there is a method that is more efficient, whether it be using 1 variable or something else.

 

Is there a way where I can echo the same variable with a different result? If that is not possible, what can I do so when  I echo the same array to get a result, it will give a different result every time based on what's already chosen so it doesn't repeat.

Is there a way where I can echo the same variable with a different result? If that is not possible, what can I do so when  I echo the same array to get a result, it will give a different result every time based on what's already chosen so it doesn't repeat.

 

nevermind, nevermind, nevermind sorry I am thinking out loud on this forum so I am being ridiculous.

THE PROBLEM WAS THAT MY TABLE AND COLUMN HAVE THE SAME NAME!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.