大家好,我一段时间以来一直在努力说服我。我想要实现的是从数据库中获取一些数据,将其存储在变量中,然后将其与存储在数组中的值进行比较。问题是它不断返回错误的输出。从SQL
下面的查询中,mysql_result $total_cat
返回值16
。一旦存储了该值,该代码将输出,echo "this value is in the array";
但不起作用。我要去哪里错了?
这是我的密码
创建一个数组来存储我的值
$lunchbox = array(12,13,14,16,20,24,33,32);
设置我的SQL数据库查询
$query = mysql_query("SELECT * FROM table WHERE id = '$the_persons_id'");
捕捉我的结果并将其存储在变量中
$total_cat = mysql_result($query,0,"category_id");
清理我的结果
$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";
我找到了答案。
这是我的代码:
创建一个数组来存储我的值
$lunchbox = array(12,13,14,16,20,24,33,32);
设置我的SQL数据库查询
$query = mysql_query("SELECT * FROM table WHERE id = '$the_persons_id'");
捕捉我的结果并将其存储在变量中
$total_cat = mysql_result($query,0,"category_id");
清理我的结果
$total_cat = str_replace("|", " ", $total_cat);
这将输出为 9 16
现在,我们运行php函数substr删除字符串的第一部分。
$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
语句求值到TRUE
并且代码已完成。
感谢所有帮助团队。
快乐编码:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句