PHP仅获取所有数据1否

时间:2016-07-21 09:09:56

标签: php mysql

我想获取所有用户但不是此用户(id = 12)如何制作此内容?

$MyID = '12';
$sql_query = mysqli_query($Conn, "SELECT * FROM users WHERE country='$MyCountry'");
while ($fetch_data = mysqli_fetch_array($sql_query)) {
    $firstname = $fetch_data['firstname'];
    echo $firstname;
}

3 个答案:

答案 0 :(得分:5)

这是最简单的形式:

$sql_query = mysqli_query($Conn, "SELECT * FROM users WHERE country='$MyCountry' and userid <> $MyID");

但这是不可取的,因为您的值未正确转义。最好使用预处理语句或mysqli_real_escape_string

$stmt = $mysqli->prepare("SELECT * FROM users WHERE country = ? and userId <> ?")
$stmt->bind_param("sd",$MyCountry, $MyId);
$stmt->execute();

答案 1 :(得分:0)

select * from user WHERE  ID!=$mycountry;

答案 2 :(得分:0)

你可以轻松地做到这一点

假设您的字段是id = 12

然后

SELECT * FROM your_table WHERE id <> 12