xerox02 Posted July 16, 2010 Share Posted July 16, 2010 <?php mysql_connect("localhost","root",""); mysql_selectdb("quotes"); $query = "SELECT DISTINCT id, quotes FROM quotes ORDER BY id DESC LIMIT 7"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); echo $row['id']. " - ". $row['quotes']; echo "<br />"; echo $row['id']. " - ". $row['quotes']; ?> My db name is: quotes My table name: quotes This code echos/prints 7 - My lizard 7 - My lizard Instead of what I want 7 - My lizard 6 - My cat Also, I'd like to note that I want to print each row one at a time. Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/ Share on other sites More sharing options...
xerox02 Posted July 17, 2010 Author Share Posted July 17, 2010 <?php mysql_connect("localhost","root",""); mysql_selectdb("quotes"); $query = "SELECT DISTINCT id, quotes FROM quotes ORDER BY id DESC LIMIT 7"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); echo $row['id']. " - ". $row['quotes']; echo "<br />"; echo $row['id']. " - ". $row['quotes']; ?> My db name is: quotes My table name: quotes This code echos/prints 7 - My lizard 7 - My lizard Instead of what I want 7 - My lizard 6 - My cat Also, I'd like to note that I want to print each row one at a time. Also, I want to add that I only want to echo 1 row of the array at a time, and not the entire array (side note: so I can put different rows in different places). Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087295 Share on other sites More sharing options...
Pikachu2000 Posted July 17, 2010 Share Posted July 17, 2010 The query is functioning properly, you're just echoing it twice here: echo $row['id']. " - ". $row['quotes']; echo "<br />"; echo $row['id']. " - ". $row['quotes']; Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087452 Share on other sites More sharing options...
Mchl Posted July 17, 2010 Share Posted July 17, 2010 I am experiencing a case of deja vu http://www.phpfreaks.com/forums/index.php/topic,303700.msg1436631.html#msg1436631 Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087459 Share on other sites More sharing options...
xerox02 Posted July 17, 2010 Author Share Posted July 17, 2010 The query is functioning properly, you're just echoing it twice here: echo $row['id']. " - ". $row['quotes']; echo "<br />"; echo $row['id']. " - ". $row['quotes']; How do I print a different row from the array? Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087543 Share on other sites More sharing options...
Alex Posted July 17, 2010 Share Posted July 17, 2010 echo $row['row name']; But you need to make sure you add it to your select query as well. Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087558 Share on other sites More sharing options...
xerox02 Posted July 17, 2010 Author Share Posted July 17, 2010 echo $row['row name']; But you need to make sure you add it to your select query as well. I am not sure what the name of my rows are, but my table contains 2 columns. It looks like this: id quotes 1 My dog 2 I had 3 I had 4 I had 5 I am 6 My cat 7 My lizard So I all I want to do is print the row with the id of 7 and 6 and 5. I also don't want to allow the query to print the same row twice (that's why I put distinct). Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087563 Share on other sites More sharing options...
Alex Posted July 17, 2010 Share Posted July 17, 2010 Oh sorry, I thought you meant other columns.. To loop through all the rows returned you can do this (you were already provided with this in another topic..): while($row = mysql_fetch_assoc($result)) { echo $row['id']. " - ". $row['quotes'] . "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087596 Share on other sites More sharing options...
xerox02 Posted July 17, 2010 Author Share Posted July 17, 2010 Oh sorry, I thought you meant other columns.. To loop through all the rows returned you can do this (you were already provided with this in another topic..): while($row = mysql_fetch_assoc($result)) { echo $row['id']. " - ". $row['quotes'] . "<br />"; } Is it kool if I msg you on aim man because I might be misinterpreted. Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087599 Share on other sites More sharing options...
Zane Posted July 17, 2010 Share Posted July 17, 2010 SELECT DISTINCT id, quotes FROM quotes Did you try switching id and quotes around maybe?.... ya know, querying distinct Quotes instead of IDs. SELECT DISTINCT quotes, id FROM quotes Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087601 Share on other sites More sharing options...
Mchl Posted July 17, 2010 Share Posted July 17, 2010 DISTINCT is a modifier for whole SELECT query, not for individual columns. Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087603 Share on other sites More sharing options...
Pikachu2000 Posted July 17, 2010 Share Posted July 17, 2010 You want to execute the query once at the beginning of the script, then use the results of it in other random places throughout the script, is that about right? Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087604 Share on other sites More sharing options...
xerox02 Posted July 17, 2010 Author Share Posted July 17, 2010 I actually got a solution, thanks a lot for the help. Needlessly to say this is a great forum with great people. Here it is: <?php mysql_connect("localhost","root",""); mysql_selectdb("quotes"); $query = "SELECT id, quotes FROM quotes WHERE id IN (7,6,5) ORDER BY id DESC"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); echo $row['id']. " - ". $row['quotes'] . "<br />\n"; ... other code ... $row = mysql_fetch_array($result); echo $row['id']. " - ". $row['quotes'] . "<br />\n"; ... other code ... $row = mysql_fetch_array($result); echo $row['id']. " - ". $row['quotes'] . "<br />\n"; ?> Sorry if I seemed ignorant, I had to make another query to make it work! Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087605 Share on other sites More sharing options...
xerox02 Posted July 17, 2010 Author Share Posted July 17, 2010 You want to execute the query once at the beginning of the script, then use the results of it in other random places throughout the script, is that about right? Pikachu2000 that's exactly what I want to do!! I am interested in the method you would use!! Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087606 Share on other sites More sharing options...
Pikachu2000 Posted July 17, 2010 Share Posted July 17, 2010 I was thinking maybe I'd store the results in another array, and pull them out as necessary. Quote Link to comment https://forums.phpfreaks.com/topic/207990-echo-mysql-row-without-repeating-distinct-doesnt-seem-to-work/#findComment-1087608 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.