How to use an array to query

50
May 19, 2019, at 1:00 PM

Is there any way I can use the array from $query and use it in another query like the $query2 below? It works but it only returns the first data of the array...

Already tried these links already but none of them worked in my case Array in SQL Query?

How to query a database with an array? WHERE = 'array()'

$query = mysqli_query($db, "SELECT * FROM patient_details WHERE discharged_date >= '2019-03-01' AND discharged_date < '2019-03-30'");
    while ($row = mysqli_fetch_array($query)){
        $result = $row['hosp_patient_no'];
        $query2 = mysqli_query($db, "SELECT deficiencies_id FROM deficiency_patient_details WHERE hosp_patient_no = '$result'");
}
Answer 1

you are using mysqli_fetch_array which will "Fetches a result row as an associative, a numeric array, or both", but your requirement says it otherwise so you should use mysqli_fetch_all which "Fetches all result rows as an associative array, a numeric array, or both".

so your syntax should look like this

$query = mysqli_query($db, "SELECT * FROM patient_details WHERE discharged_date >= '2019-03-01' AND discharged_date < '2019-03-30'");
while ($row = mysqli_fetch_all($query)){
    $result = $row['hosp_patient_no'];
    $query2 = mysqli_query($db, "SELECT deficiencies_id FROM deficiency_patient_details WHERE hosp_patient_no = '$result'");

}

infact i would suggest you to do this in a single query using subquery.

READ ALSO
MySQL query with COUNT and ORDER BY and hidden results

MySQL query with COUNT and ORDER BY and hidden results

Im trying to get a query correct where I can display the data by State with each of the States cities with stores in it with the COUNT (Number of stores in each city)Any help is greatly appreciated

55
Only allow users on page if IP address is approved

Only allow users on page if IP address is approved

How can I make an HTML (and CSS/PHP/JavaScript) document which only allows certain IP addresses on a page?

66
What is the proper Query and PHP to print an archive of the database based on Year, Month?

What is the proper Query and PHP to print an archive of the database based on Year, Month?

I've found plenty of examples that either don't show the query, or don't show the codeI'm looking to format the output as such:

39
control structures &amp; functions- filter function [on hold]

control structures & functions- filter function [on hold]

You are given a list of strings such as

61