xerox02 Posted July 9, 2010 Share Posted July 9, 2010 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/ Share on other sites More sharing options...
Mchl Posted July 9, 2010 Share Posted July 9, 2010 SELECT DISTINCT id, quotes FROM quotes ORDER BY DESC LIMIT 1 ?? Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083443 Share on other sites More sharing options...
bh Posted July 9, 2010 Share Posted July 9, 2010 Hi, Post your table structure. Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083446 Share on other sites More sharing options...
xerox02 Posted July 9, 2010 Author Share Posted July 9, 2010 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" Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083447 Share on other sites More sharing options...
Mchl Posted July 9, 2010 Share Posted July 9, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083452 Share on other sites More sharing options...
xerox02 Posted July 9, 2010 Author Share Posted July 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083455 Share on other sites More sharing options...
Mchl Posted July 9, 2010 Share Posted July 9, 2010 while($row = mysql_fetch_array($result)) { echo $row['id']. " - ". $row['quotes']; echo "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083458 Share on other sites More sharing options...
xerox02 Posted July 9, 2010 Author Share Posted July 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083463 Share on other sites More sharing options...
Mchl Posted July 9, 2010 Share Posted July 9, 2010 Well? The code above should display all rows fetched by this query. Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083468 Share on other sites More sharing options...
xerox02 Posted July 9, 2010 Author Share Posted July 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083469 Share on other sites More sharing options...
bh Posted July 9, 2010 Share Posted July 9, 2010 Put the results into a php array... Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083471 Share on other sites More sharing options...
xerox02 Posted July 9, 2010 Author Share Posted July 9, 2010 Put the results into a php array... When it echos it keeps repeating tho. Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083472 Share on other sites More sharing options...
xerox02 Posted July 9, 2010 Author Share Posted July 9, 2010 I think I got everything to work, I just need to make it so when I echo something it does not echo the same thing twice (like it does with the above code with the adjustments.) Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083501 Share on other sites More sharing options...
bh Posted July 9, 2010 Share Posted July 9, 2010 example then store what you echos... and the next echo check whether you did echo that line... Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083503 Share on other sites More sharing options...
xerox02 Posted July 9, 2010 Author Share Posted July 9, 2010 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083743 Share on other sites More sharing options...
xerox02 Posted July 9, 2010 Author Share Posted July 9, 2010 Thanks everyone, I got it to work. I just had to make variables for every row. Is there a way to do it more efficiently with just 1 variable or any other way? Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083755 Share on other sites More sharing options...
xerox02 Posted July 9, 2010 Author Share Posted July 9, 2010 bump Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083823 Share on other sites More sharing options...
Mchl Posted July 9, 2010 Share Posted July 9, 2010 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 />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083833 Share on other sites More sharing options...
xerox02 Posted July 10, 2010 Author Share Posted July 10, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083888 Share on other sites More sharing options...
xerox02 Posted July 10, 2010 Author Share Posted July 10, 2010 lol I am sorry, I just realized that I am calling an array that's why it's happening I think. Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083895 Share on other sites More sharing options...
xerox02 Posted July 10, 2010 Author Share Posted July 10, 2010 lol I am sorry, I just realized that I am calling an array that's why it's happening I think. nvm actually, mannn. Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083912 Share on other sites More sharing options...
xerox02 Posted July 10, 2010 Author Share Posted July 10, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083924 Share on other sites More sharing options...
xerox02 Posted July 10, 2010 Author Share Posted July 10, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/207220-echo-mysql-row-without-repeating-and-in-descending-order/#findComment-1083934 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.