如何使用php oop从表中选择值?

如何使用php oop从表中选择值?,php,mysql,oop,Php,Mysql,Oop,我试图从MySQL表中选择一些值,如下所示: <?php $this_id=(55,66,77); $cate= find_by_sql('select * from tages where id in ( "'.$this_id.'") '); //function find_by_sql() get values and put in array () ?> <?php foreach($cate as $cates1): ?> <?php echo $c

我试图从MySQL表中选择一些值,如下所示:

<?php
$this_id=(55,66,77);

$cate= find_by_sql('select * from tages where id in ( "'.$this_id.'") '); 
//function find_by_sql() get  values and put in array ()
?>
<?php foreach($cate as $cates1): ?>
<?php echo $cates1->tage_name; 
// tage_name is var in class 
?>
<?php endforeach; ?>

但是,它只选择id为55的位置。

将其更改为这样

<?php
$this_id = "55,66,77";

$cate= find_by_sql('select * from tages where id in ( ' . $this_id . ') '); 
//function find_by_sql() get  values and put in array ()
?>
<?php foreach($cate as $cates1): ?>
<?php echo $cates1->tage_name; 
// tage_name is var in class 
?>
<?php endforeach; ?>

因为您在变量周围使用引号,所以我假设这不是一个预先准备好的语句。在这种情况下,您必须实际提供一个字符串才能使sql有效。换句话:

$this_id=('55,66,77'); // notice the single-quotes

您需要将其设置为数组$this_id=array55,66,77;您可以用方法类替换所有函数调用,但简单的事实是,根据结束标记和开始标记判断,您的查询可能与标记混合在一起。您的代码可能使用类,但这并不意味着它是OOproject@Fred-我恐怕不能同意,因为他没有使用预先准备好的语句。用逗号分隔的数字字符串是正确的书写方式:。应该是“55,66,77”@FélixGagnon Grenier你可能是对的,但那只是一个评论。除非他们答对了,否则你可能想在下面的答案中贴出同样的答案。在那一点上我不会和你争论;-我可能会说:你可能需要将其作为一个数组,因为其他表中的值$this_id 55,66,44我根据你之前的评论更新了我的代码。vary good谢谢男士:问题解决了。问题是$this_id是从其他表中获取值,而他的字段中的值是:55,66,77可能是foreach我写得不好?嗯。问题是您没有完全粘贴代码。我们无法知道$this_id来自另一张桌子,它真的改变了一切。无论如何,请检查您的问题是否重复,您将学会如何执行此操作是的,对不起,我的问题不清楚,因为这里有许多代码我无法输入。不要全部输入,只需输入相关行$this\u id即可