[英]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. 我正在尝试使用PHP和MySQL创建注释系统。 I have made a table named "comment" which stores the details
我制作了一个名为“ comment”的表,用于存储详细信息
id | date |product_id | user_name | email_id | comment | | | | | 1 |2013-05-07 | 10001 | jabong | jabong@jabong.com | this is good product 2 |2013-05-07 | 10001 | john | john@gmail.com | I bought this product
and so on 等等
Now I want to select and print all those rows which have product_id=10001. 现在,我要选择并打印所有具有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.
因此,请帮助我获取正确的代码。
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>';
}
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)) 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. 第二个问题是您在每个循环中都覆盖了$ comment变量,因此即使循环两次,您也只会得到1条记录。
remove this: $comment = $data["comment"];
change this: <td>Product Review: '.$comment.'</td>
to this: <td>Product Review: '.$data["comment"].'</td>
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. 因此,当您在执行
echo $comment
它会显示最后一个。
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 />';
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.