Custom queries using arrays and implode function in php

35
January 12, 2019, at 09:20 AM

I am coding a game site. On this site, every question should be displayed to the user only once. Questions ID's are stored in a one field:

name of field in table: hidden_questions

hidden_questions values :1,2,3,17

I'm trying to get the IDs from the database and store them in an array using the implode function:

$q1= array($data['question_id']);
$q2= implode(',', $q1); // returns 1,2,3,4,5

And I want to display a non-repeat question using the following query:

$sql = "SELECT * FROM question WHERE question_id!=('".$q2."') LIMIT 1";

This code does not work and questions with the ID stored in the hidden_questions field are displayed. please guide me.

Answer 1

I see that someone already wrote a comment about the solution, but I still write it down for you.

You are on the right way and I understand what you want to do. But, MySQL does not have something like != in this syntax.

Maybe you can try to use the NOT IN

$sql = "SELECT * FROM question WHERE question_id NOT IN (".$b.") LIMIT 1";

Also, why do you use the LIMIT 1? According to your explanation it looks like that you want to show multiple questions.

Disclaimer: I did not test the code, maybe you needto play some with the quotes. But this should be the solution

READ ALSO
look for a tool to consume redis and virsialize the process

look for a tool to consume redis and virsialize the process

I am looking for a open source software to configure php scripts to consume redis list,and visualize the whole process and monitor the whole process

21
How to catch an exception for sodium_crypto_box

How to catch an exception for sodium_crypto_box

I am trying to see if the message gets corrupted in the middle I should be able to get an error but all I am seeing is a white page

7
How to install v8js for php5.6 on Centos 6?

How to install v8js for php5.6 on Centos 6?

I follow step by step from this : https://gistgithub

29