使用WHERE子句

时间:2016-10-04 04:58:45

标签: mysql sql

我将此代码翻录到某个地方但我在尝试使用WHERE子句过滤结果时遇到问题。我想要做的是包括一个WHERE子句,如WHERE name =' John'在语句中只获取与人名相关的特定记录,而不是代码当前所做的每个人的整个结果。这是我的代码:

$query=mysql_query("SELECT @rownum := @rownum + 1 AS urutan,t.*
FROM customer t, 
(SELECT @rownum := 0) r");
$data = array();
while($r = mysql_fetch_assoc($query)) {
$data[] = $r;
}
$i=0;

2 个答案:

答案 0 :(得分:1)

你真的不应该使用mysql_ *函数

$query = "SELECT @rownum := @rownum + 1 AS urutan,t.*
FROM customer t, 
(SELECT @rownum := 0) r  WHERE t.name=?"

$stmt = mysqli_prepare($link, $query)
mysqli_stmt_bind_param($stmt,"s","Eric");
mysqli_stmt_execute($stmt);

答案 1 :(得分:0)

WHERE子句属于最后:

$query = mysql_query("SELECT @rownum := @rownum + 1 AS urutan, t.*
FROM customer t, 
(SELECT @rownum := 0) r WHERE t.name = 'John'");