-2

I'm trying to select some values from a MySQL table like this :

<?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; ?>

But, it's only selecting where the id is 55.

rrirower
  • 4,338
  • 4
  • 27
  • 45
  • You need to make it as an array `$this_id= array(55,66,77);` – Funk Forty Niner Jan 12 '15 at 16:15
  • You can replace all of the funtion calls by method class, but the simple fact of the matter is that you're query probably is mixed in with markup judging by the closing and opening tags). Your code might _use_ classes, but that doesn't make it an OO project – Elias Van Ootegem Jan 12 '15 at 16:15
  • @Fred-ii- I'm afraid I'll have to disagree, since he's not using prepared statements a string litteral of numbers separated by commas is the right way to write it :). it should be `'55,66,77'` – Félix Adriyel Gagnon-Grenier Jan 12 '15 at 16:20
  • 1
    @FélixGagnon-Grenier You may be right, but that was just a comment. You may want to post that same one in the "answer" below, unless they've got it right. I won't argue with you on that point ;-) I should have probably said: *"You **may** need to make it as an array"* – Funk Forty Niner Jan 12 '15 at 16:22

2 Answers2

0

change it to be like this.

<?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; ?>
Jonathan
  • 2,778
  • 13
  • 23
0

since you're using quotes around your variable, I assume this is not a prepared statement. In that case, you have to actually present a string for your sql to be valid. just change that line:

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