Pankaj
Pankaj

Reputation: 195

Select more than one row with same value in one column in mysql?

I'm trying to make a comment system using PHP and MySQL. I have made a table named "comment" which stores the details

id  | date      |product_id | user_name | email_id          | comment
    |           |           |           |                   |
1   |2013-05-07 |  10001    |  jabong   | [email protected] | this is good product
2   |2013-05-07 |  10001    |  john     | [email protected]    | I bought this product 

and so on


Now I want to select and print all those rows which have product_id=10001. I'm using the following code to print the details

  $comment="";
  $id=$_GET['id'];//this is the product_id which is taken from URL
  $query=mysql_query("SELECT * FROM comment WHERE product_id='$id'");
  while($data = mysql_fetch_array($query))
    {
        $name = $data["user_name"];
        $email = $data["email_id"];
        $comment = $data["comment"];
        $date=strftime("%d %b, %Y", strtotime($data["date"]));

        $comment.='<table>
                      <tr>
                        <td>Date: '.$date.'</td>
                      </tr>
                      <tr>
                        <td>Name: '.$name.'</td>
                      </tr>
                      <tr>
                        <td>Email_id: '.$email.'</td>
                      </tr>
                      <tr>
                        <td>Product Review: '.$comment.'</td>
                      </tr>
                    </table><hr />'; 

and then I'm echoing them in the body section. but I'm getting only one result. so please help me to get the right code.

Upvotes: 1

Views: 1190

Answers (3)

Pankaj
Pankaj

Reputation: 195

There was a small problem with my code. I was using the same variable twice and also i didn't use the concatenate symbol. But now it is solved---

      $review="";
      if (isset($_GET['id']))
  {
      $id=mysql_real_escape_string(trim($_GET['id']));
      $query=mysql_query("SELECT * FROM comment WHERE product_id='$id'");
      $review.='<table width="70%" cellpadding="6px" cellspacing="0" >';
      while($data=mysql_fetch_array($query))
        {
            $name = $data["user_name"];
            $email = $data["email_id"];
            $comment = $data["comment"];
            $date=strftime("%d %b, %Y", strtotime($data["date"]));
            $review.='<tr>
                        <td>
                          <table width="100%" border="0" style="border:1px solid; border-color:#05fdee; background-color:#e4f2f1;">
                              <tr>
                                <td rowspan="2" width="100px"> <img src="profile.png" /> </td>
                                <td align="left"><b> '.$name.' </b> says -</td>
                                <td align="right"><b> '.$date.' </b></td>
                              </tr>
                              <tr>
                                <td colspan="2">'.$comment.'</td>
                              </tr>
                          </table>
                        </td>
                      </tr>';

        }// while loop ends here
        $review.='</table>'; 
  } 

Upvotes: 1

jcho360
jcho360

Reputation: 3759

you have this line:

$comment = $data["comment"];

that's overwriting this one:

$comment.='<table>
                      <tr>
                        <td>Date: '.$date.'</td>
                      </tr>
                      <tr>
                        <td>Name: '.$name.'</td>
                      </tr>
                      <tr>
                        <td>Email_id: '.$email.'</td>
                      </tr>
                      <tr>
                        <td>Product Review: '.$comment.'</td>
                      </tr>
                    </table><hr />'; 

so when you're making the echo $comment it's displaying the last one.

Change the variable and DONE

something like:

 $comment_sql = $data["comment"];
....
    $comment.='<table>
                          <tr>
                            <td>Date: '.$date.'</td>
                          </tr>
                          <tr>
                            <td>Name: '.$name.'</td>
                          </tr>
                          <tr>
                            <td>Email_id: '.$email.'</td>
                          </tr>
                          <tr>
                            <td>Product Review: '.$comment_sql.'</td>
                          </tr>
                        </table><hr />'; 
...

Upvotes: 0

John Petrilli
John Petrilli

Reputation: 191

First most important thing, clean your data.

$id=mysql_real_escape_string(trim($_GET['id']));
$query=mysql_query("SELECT * FROM comment WHERE product_id='$id'");

while($data = mysql_fetch_array($query))

The second problem is you are overwriting the variable $comment in each loop, so you are only getting 1 record, even though you are looping twice.

remove this: $comment = $data["comment"];
change this: <td>Product Review: '.$comment.'</td>
to this: <td>Product Review: '.$data["comment"].'</td>

Upvotes: 0

Related Questions