0

I have a basic query as follows

$query = "SELECT * FROM files WHERE (subject_code = '".$subject_code."' )";

I have to modify this query to create a query where we have multiple subject codes fetched from an array.

$query1="SELECT subject_code FROM subject WHERE (sbranch = '".$branch."' )";
$result = mysqli_query($bmysqli, $query1);

Now result holds all the required subject_codes say result[0]=SC1,result[1]=SC2 etc....

Now I want to the original query to be modified something as follows

$query = "SELECT * FROM files WHERE (subject_code = SC1 OR subject_code = SC2 .....)";

Note that i don't know what are the values of SC1 SC2 etc.It depends on the branch and is inside $result.

How can i accomplish this?

6
  • 1
    stackoverflow.com/questions/20203063/mysql-where-id-is-in-array Commented Jul 7, 2017 at 11:25
  • See this sql_in Commented Jul 7, 2017 at 11:26
  • So should i use SELECT * FROM files WHERE subject_code IN ('".$result."')"); Should is use mysql fetch array? Commented Jul 7, 2017 at 11:28
  • Check my answer below. I have updated it as result is an associative array. Commented Jul 7, 2017 at 12:02
  • okay, I have added a line to typecast object to array. Commented Jul 7, 2017 at 12:17

7 Answers 7

1

You can use a subquery like this:

$query = 'SELECT * FROM files 
WHERE subject_code IN (
    SELECT subject_code FROM subject WHERE sbranch = "' . $branch . '"
)';
Sign up to request clarification or add additional context in comments.

Comments

1

You can use implode to convert array to string comma delimited

like below:

$branch_array = array('subject1', 'subject2', 'subject3');
$branch = implode("','", $subject_array ); 

$query1="SELECT subject_code FROM subject WHERE sbranch  in ('".$branch."')";

result:

 'subject1', 'subject2', 'subject3' 

Hope that resolve your issue

Comments

1

You can use Mysql In operator Like this

SELECT * FROM files WHERE subject_code in ('SC1','SC2')";

You can achieve ('SC1','SC2') by using implode php function,
like this:

$subCodes = implode("','", $subject_array ); 
SELECT * FROM files WHERE subject_code in ('".$subCodes ."')"

1 Comment

But i dont know SC1,SC2 its in $result.How to convert $result to above format?
1
$query1="SELECT subject_code FROM subject WHERE (sbranch = '".$branch."' )";
$result = mysqli_query($bmysqli, $query1);
$codes=(array)$result; //this will convert it in normal array. old school typecasting ;)
$codes=array_values($codes);
$query = "SELECT * FROM files WHERE subject_code in (".implode(",",$codes).")";
//considering result is an object of subject codes.

CODE UPDATED ABOVE as it is an object.

You don't need to append to the first query. Just build a new query using MySQL in operator and php implode function.

6 Comments

This is not working because $result is associative array.Can you help me with this..
Edited the answer. Please mark my answer as correct if it works. Thanks :)
Error array_values() expects parameter 1 to be array, object given
Out of all the answers this is exactly what i want but still cannot make it to work..
So when you said that it is an associative array which is not. it is an object. Answer updated again.
|
0
SELECT * FROM files WHERE subject_code in ('SC1','SC2')

2 Comments

But i dont know SC1,SC2 its in $result.How to convert $result to above format?
$ids = join("','",$arrayOfValues); $sql = "SELECT * FROM files WHERE subject_code IN ('$ids')";
0

You can amend your php array to comma delimited string of SubjectCodes.

then:

$query1="SELECT * FROM files WHERE subject_code in (".$NewString.") ";

$NewString will look something like this:

"'SC1', 'SC2', 'SC3'"

2 Comments

The $result array has the subjects but how to convert $result to above format?
@ElectronicsForKTU implode( "," , $YourArray)
0
 $result = array(0=>'SC1',1=>'SC2');  //this is your array
 $branch = implode("','", $result );
 $query .= "
   SELECT * FROM files WHERE subject_code in ('".$branch."') "; //this is query

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.