Php 如何查看来自数据库的结果是否与数组中的my值匹配

Php 如何查看来自数据库的结果是否与数组中的my值匹配,php,mysql,sql,arrays,Php,Mysql,Sql,Arrays,嗨,伙计们,一段时间以来,我一直在努力解决这个问题。我试图实现的是从数据库中获取一些数据,将其存储在变量中,并将其与存储在数组中的值进行比较。 问题是它总是返回错误的输出。从下面的SQL查询中,mysql\u结果$total\u cat返回值16。存储此值后,代码将输出echo“此值在数组中”但它不工作。 我哪里做错了 这是我的代码 创建一个数组来存储我的值 $lunchbox = array(12,13,14,16,20,24,33,32); $lunchbox = array(12,13,

嗨,伙计们,一段时间以来,我一直在努力解决这个问题。我试图实现的是从数据库中获取一些数据,将其存储在变量中,并将其与存储在数组中的值进行比较。 问题是它总是返回错误的输出。从下面的
SQL
查询中,
mysql\u结果$total\u cat
返回值
16
。存储此值后,代码将输出
echo“此值在数组中”但它不工作。
我哪里做错了

这是我的代码

创建一个数组来存储我的值

$lunchbox = array(12,13,14,16,20,24,33,32);
$lunchbox = array(12,13,14,16,20,24,33,32);
设置我的SQL数据库查询

$query = mysql_query("SELECT * FROM table WHERE id = '$the_persons_id'");
$query = mysql_query("SELECT * FROM table WHERE id = '$the_persons_id'");
捕获我的结果并将其存储在变量中

$total_cat = mysql_result($query,0,"category_id");
$total_cat = mysql_result($query,0,"category_id");
清理我的结果

$total_cat  = str_replace("|", " ", $total_cat);
$total_cat  = str_replace("|", " ", $total_cat);
检查我的sql结果是否与存储在数组中的任何结果匹配

if (in_array($total_cat, $lunchbox)) {
    echo "this value is in the array";
}else{
    echo "this value is not in the array";
if (in_array($total_cat, $lunchbox)) {
    echo "this value is in the array";
}else{
    echo "this value is not in the array";
我找到了答案

这是我的代码:

创建一个数组来存储我的值

$lunchbox = array(12,13,14,16,20,24,33,32);
$lunchbox = array(12,13,14,16,20,24,33,32);
设置我的SQL数据库查询

$query = mysql_query("SELECT * FROM table WHERE id = '$the_persons_id'");
$query = mysql_query("SELECT * FROM table WHERE id = '$the_persons_id'");
捕获我的结果并将其存储在变量中

$total_cat = mysql_result($query,0,"category_id");
$total_cat = mysql_result($query,0,"category_id");
清理我的结果

$total_cat  = str_replace("|", " ", $total_cat);
$total_cat  = str_replace("|", " ", $total_cat);
这将输出为
9 16

现在我们运行php函数来删除字符串的第一部分

$total_cat = substr($total_cat, 1);
这将为我们提供
16的输出

现在,我们检查SQL结果是否与存储在数组中的任何结果匹配

if (in_array($total_cat, $lunchbox)) {
    echo "this value is in the array";
}else{
    echo "this value is not in the array";
if (in_array($total_cat, $lunchbox)) {
    echo "this value is in the array";
}else{
    echo "this value is not in the array";
现在我的
IF
语句的计算结果为
TRUE
,代码就完成了

感谢所有的帮助团队


快乐编码:)

您的意思是表中只有一列的查询参数值为16吗?我很好奇,因为我看到你正在做一个
select*
category\u id在数据库中是什么样子的?你的“清洁”让我猜想,它可能看起来像1 | 2 | 16。。。因此,实际上,您可以通过“清理”它来生成“1 2 16”,它不在数组中……旁注:
$total_cat=str_replace(“|”),“”,$total_cat)
您正在用空格替换
|
,而您的数组不包含任何空格。在执行此操作时,您需要执行
$total_cat=str_replace(“|”)、“”、$total_cat)
或使用
trim()
。表中有不止一列,但我正在测试屏幕上显示的第一个值,找到了答案。谢谢大家的帮助。我所需要做的就是使用php函数substr,所以它看起来像这样的
$total\u cat=substr($total\u cat,1)
echo“$total\u cat”
结果将显示为16,使我的最终结果计算为true,从而在数组中找到我的值。:)如果是解决方案,请接受您的答案。:)