1

If i create an array from results returned in a mysqli query is there a way to select and use only one specific row from the array?

$info= array();
while($row = mysqli_fetch_assoc($query)) {
    $info[] = array(  
        'id' => $row['id'],  
        'location' => $row['location']
    );  
}

How would i go about displaying only a single row from this array where the id equals a variable like $id?

1 Answer 1

1

In your loop you could just do something like:

if ($id == $row['id']) {
    $info[] = $row;
}

However it would make more sense to me to just update your query.

SELECT cols FROM t1 WHERE id = :id

Using $id as a parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

Im using the array result in a different while loop using if(in_array) So i need all the results and not just the one the query would return. That part of my code works fine ill try your suggestion thanks.

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.