Reputation: 1676
Do you have to use a loop to extract the value from a query array.
Query:
$fetchRegion = Singlequery("SELECT region FROM regions WHERE id = :id LIMIT 1",
array('id' => $_GET['region']),
$conn);
This is my array:
array(1) {
[0]=>
array(1) {
["region"]=>
string(10) "South West"
}
}
I want to take the value and use it in another query, I didn't know if I had to use a foreach for example to get the value to use in my next query. The other stackoverflow questions that I saw used a loop
Upvotes: 0
Views: 322
Reputation: 898
If i understand your question correctly and you want to acces to value then access it like so:
$fetchRegion[0]['region'];
You don't need to use foreach or any other loop since it will return at most one element because LIMIT 1 you used in query.
Upvotes: 1
Reputation: 2076
If you're certain that your result is going to look like that, reset(reset($fetchRegion))
will give you the value 'south west'. It will behave badly if you don't get that exact format back though - for example, if the query does not return a row.
Upvotes: 0
Reputation: 2905
No, there is no need to use a loop with that query because it will not return more than a single row. Instead, just check that you got a row back (it could return no results), and then use it.
Upvotes: 0
Reputation: 13728
Using loop :-
foreach($fetchRegion as $v) {
$var = $v["region"];
}
or you get directly like:-
echo $fetchRegion[0]["region"];
Upvotes: 1