如果列'id'具有类似1,2,22等的值,但其形式为:1 * 2 * 3#7 * 3#4 * 5 * 21#4 * 3,则下面的查询工作正常。现在,如果在对应的列'id'中找到$ x(格式为1,2,12等),则该行应存储在$ row中。查询$ q应该如何实现呢?

$q=mysqli_query($db,"select * from table1 where id='$x' order by cid DESC");
$row=mysqli_fetch_assoc($q);

最佳答案

跳至UPDATE#4获得解决方案(应该有同样问题的人)

如果我正确理解您的问题...您想检查$ x是否为数字?然后,用以下代码替换您提供的代码:

if(!preg_match("@[^0-9]@i", $x))
{
    $q=mysqli_query($db,"select * from table1 where id='$x' order by cid DESC");
    $row=mysqli_fetch_assoc($q);
}


编辑:这是什么......它检查$ x变量的值是否有其他字符,然后在其中输入数字。如果没有其他字符,则只有数字,则将执行if语句中的代码。

更新:

我第一次没收到你的问题。让我们再试一次:

select * from table1 where id = '$x' AND id NOT LIKE '%#%' order by cid DESC


更新#2:

select * from table1 where id LIKE '%$x%' order by cid DESC


更新#3:

select * from table1
where
id LIKE '$x*%' OR
id LIKE '%*$x' OR
id LIKE '%*$x*' OR
id LIKE '%#$x#' OR
id LIKE '%#$x*' OR
id LIKE '%*$x#'
order by cid DESC


更新#4(如果id只是一个数字):

select * from table1
where
id = '$x' OR
id LIKE '$x*%' OR
id LIKE '%*$x' OR
id LIKE '%*$x*' OR
id LIKE '%#$x#' OR
id LIKE '%#$x*' OR
id LIKE '%*$x#'
order by cid DESC

关于php - 如果某列中存在某个变量,则查询以列出该行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24640857/

10-14 13:50
查看更多