selecting data into an array with PHP and mySQL

Dear Experts,
I need to select to some data from my database but this time I think I need to use an array.

I wrote something like this but it is not working?

$sql = "SELECT id,onay FROM urunler where id between 271 and 290";
$info = array();
$result = $mysqli->query($sql);

    while ($row_user = mysql_fetch_assoc($sql)){
    $info[] = $row_user; 	
	}
	
	foreach ($info as $user) {
    echo "Id: {$user[id]}<br />"
       . "Name: {$user[onay]}<br />";
}

Open in new window

Also, I wonder how can I call a specific variable from this array?
For example if I'd like to see what is inside id 271, should I write it like this?
echo $user[onay][271];

Open in new window

LVL 1
BRMarketingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
No that won't work. The indices of the array are sequential starting at 0 where 0 is the first row retrieved 1 the second etc. There is no link between the indices of the array and the data in the array.

To find the the user with id 271 you need to loop through the array and look for it.
Each row contains an associative array of field_name=>value

So, you would need to do something like this

foreach ($info as $user)  {
   if ($user['id'] == 271) {
      // found it
      echo $user['onay'];
   }
}

Open in new window

The above may not be the right code for what you want to do - if you give us a bit more info we can tailor it more to you what you need.
Chris StanyonWebDevCommented:
You coud use the array_filter to get at the record you need. You also don't need the while loop - you're effectively just copying an element out of one array into another:

$sql = "SELECT id, onay FROM urunler where id between 271 and 290";
$result = $mysqli->query($sql);
$info = $result->fetch_all(MYSQLI_ASSOC);

// Loop your users
foreach ($info as $user):
    printf("Id: %s <br> Name: %s <br>", $user['id'], $user['onay']);
endforeach;

// Retrieve a specific user
$myUser = array_filter($info, function($arr){
    return $arr["id"] == 271;
});

var_dump($myUser);

Open in new window

BRMarketingAuthor Commented:
Thank you both,
i will try to test Chris's code tomorrow

What i want to do is to select the "id" and "onay" columns from the table as an array and use the necessary id when i need it. Otherwise i think i need to write diffrent sql statement every time when i need it.

I think:
If sql brings only one row, it is easy to assign the value to a variables. However when it brings more than one row i need to use arrays. Is that correct?
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Chris StanyonWebDevCommented:
When you run a query against your Db and select more than one column you will get an array that represents each row. If you have more than one row, you can either loop over them one-by-one or pull them all in in one go. In my code, it pulls them all in in one go and assigns them to the $info array. This $info variable then contains an array or arrays, that you can either loop over (foreach) or select a specific record (array_filter).

If you're going to be pulling out records from the array more than once, then it makes sense to put the array_filter call in your own function can call that each time instead.

Hope that makes sense, but come back if not.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Olaf DoschkeSoftware DeveloperCommented:
In the aspect of reading data for id=271 from the already fetched array, are you aware you're asking for something like a query on the result of a query? Something they call LINQ (language integrated query) in DotNet.

Wanting to fetch a single id is a very simple such query. If that's all you need you might consider using an extra associated array where you add full rows as elements and associate them with the id, instead of just appending them, just change your code in the line

$info[$row_user['ID']] = $row_user;

Open in new window


And afterward, $info[271] will give you the row with id 271.

You might of course also just query for id 271 in the first place.

Bye, Olaf.
BRMarketingAuthor Commented:
Dear All,
Thank you for the answers
I now understand how silly it looks like :)
Because it's already an array in the first place
Thank you all
You teach me a lot
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.