0

I am trying to get values from an array that is stored in a column in my database. I have this code so far:

<?php $disciplines = (json_decode($traveler['disciplines'])); ?>
<?php $disciplinename = implode(' OR id = ',$disciplines); ?>
<?php $name = "SELECT * from the_discipline WHERE (id = 86 OR id = 118)";
$dname = $obj->queryResult($name);

Everything works fine up to a point. I am able to get the array that looks like this in the database "["86", "118"]" and print it out to look like this "86 OR id = 118" (that is the $disciplinename variable) I am then trying to put it in an SQL statement that looks like this "SELECT * from the_discipline WHERE (id = ".$disciplinename.")";. When I do that statement my page won't load and if I type out the statement without the variable like so "SELECT * from the_discipline WHERE (id = "86 OR id = 118")"; it works fine. I have rearranged things and tried many variations of the above code but I can't get anything to work properly. Is there something I'm missing or don't know about using implode() with SQL? Any input or pointers would be much appreaciated.

EDIT*

I have also tried this as suggested below:

<?php $disciplines = (json_decode($traveler['disciplines'])); ?>
<?php $disciplinename = implode(', ',$disciplines); ?>
<?php $name = "SELECT * from the_discipline WHERE id IN (".$disciplinename.")"; 
$dname = $obj->queryResult($name);
?>
10
  • better use .. WHERE id IN (..comma-separated-list..); Commented Aug 6, 2020 at 21:03
  • @ИгорьТыра good idea but that isn't working either. Nothing loads still unfortunately. Commented Aug 6, 2020 at 21:14
  • did you change the line with implode too? Commented Aug 6, 2020 at 21:15
  • @ИгорьТыра I believe I changed it correctly but I showed what I did above just in case. Commented Aug 6, 2020 at 21:19
  • just look into apache logs to see the actual script error.. it may be something about database connection. Commented Aug 6, 2020 at 21:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.