0

how can i add result of multiple queries in a single array so it makes it easier to later output.

for example,

$newArray = array("mike", "john");
while ($newArray !== false){
    $sql = "Select * from accounts where name = '$newArray'";
    $result = mysqli_query($db_connection,$sql);
}

 while ($row = mysqli_fetch_assoc($result)) {
    printf $row['firstName'];
    echo "----";
    printf $row['num_accounts'];
    echo "----";
    prinf $row['location'];
    echo "----";
    echo "<br>";
}

i am sure i am missing something that obvious. but the idea is that i want to setup a loop to read from an array and then execute the query and store the results. each time query executes the condition is different but from same table, , but then keep adding the results to single output, making it easier to output in the end.

1
  • I would recommend reading some basic tutorial about MySQL queries to get better acquainted with the technology's capabilities. There are so many things you can do. Commented Mar 5, 2018 at 14:48

2 Answers 2

1

you could avoid the while simple using in clause based on list of values you need

$newList = "'mike', 'john'";

$sql = "Select * from accounts where name IN (" .$newList .")";

but you should not use php var in sql ..you are at risk for sqlinjection .. for avoid this you should take a look at your sqldruver for binding param ..

but if you want append the result for different where condition on same table you should take a look at UNION clause

$sql = "Select * from accounts where name IN (" .$newList .") 
        UNION 
        Select * from accounts  where you_col > 10";
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for prompt response. just out of curiosity, how will this loop through the array to query for different conditions?
If you use the in clause you don't need loop for select result related to the values in the list .. but if you want select the same table with different where clause you should take a look at UNION clause for append several query (with correspondig column data type and number.. answer updated .. with a brief sample
Quite like this solution. It will always be faster executing via SQL rather than php. You can implode(','$newArray) to easily get the data that this query requires.
0

There's a few things wrong with your code..
1. You are inputting a full array into your query.
2. You're doing no preparations on your query thus it's vulnerable to sql injection if $newArray is human input data.
3. Your logic from looping thru the data, if you managed to get a single query correct, would always loop thru the last query. You need to move the $row = mysqli_fetch_assoc($result) stuff inside your actual loop.

To get this code working it would look like so:

$newArray = array("mike", "john");
foreach($newArray as $name){
    $sql = "Select * from accounts where name = '{$name}'";
    $result = mysqli_query($db_connection,$sql);

    while ($row = mysqli_fetch_assoc($result)) {
        printf $row['firstName'];
        echo "----";
        printf $row['num_accounts'];
        echo "----";
        prinf $row['location'];
        echo "----";
        echo "<br>";
    }
}

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.