Jump to content

Echo Mysql row without repeating; Distinct doesn't seem to work.


xerox02

Recommended Posts

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

Link to comment
Share on other sites

<?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).

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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 />";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.