how to select data from mySQL database and assign it to a variable?

Dear Experts,
I select data from my database like this

$sql = "SELECT _key,urun,tarih FROM order WHERE _key between 75 and 120";

$res = $mysqli->query($sql);

if ($res->num_rows > 0) {
    while($row = $res->fetch_assoc()) {
	$key= $row["_key"];	
	$urun= $row["urun"];
	$tarih= $row["tarih"];
	if ($key=="98") {$newvariable98=$urun ;}
	if ($key=="106") {$newvariable106=$urun ;}
	if ($key=="120") {$newvariable120=$urun ;}

Open in new window

since $row is an array, is there a better way to select key 98,106,120 data? Thank you
Who is Participating?

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

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.

Please clarify what you are after, $res gets the query results
You can use hash or array reference of the results.
Adding the where clause to your SQL query identifying the keys specifically will narrow down your response.

_key in (98,106,120)

You could build your SQL if needed before executing.

BRMarketingAuthor Commented:
Dear Experts,
What I want to learn is the "urun" coloumn of the key 98 and key 106 and 120.
My code works perfectly fine but I wonder,
how would you write it to get that information?

would you use if statement or is there a way to do it with arrays?
I do not understand what you are after,
Your assignment step seems unnecessary.
I would check if the key is

Case . $row['_key'] i
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Julian HansenCommented:
In answer to your question, No,

The returned row from fetch_assoc() is an associative array of fieldname => value pairs. To check for a specific field value you have to check that field for each row you get.

I would ask what it is you are trying to do - I think there might be a better way of doing what you want but I would need more info.

For instance - why not specifically search for those keys in the Query
SELECT _key,urun,tarih FROM order WHERE _key in(96,108,120)

Open in new window

Now the only records returned will be the ones that match those keys.

The next question is what do you want to do with your data - for instance you could do this
$data = array();
while($row = $res->fetch_assoc()) {
    $data[$row['key_']] = $row;

Open in new window

Now you have an array that is indexed on the key_ field and can be accessed like so
echo $data[96]['urun'];

Open in new window

You would need to put in a check to see if the key exists first in case it was not found in the db but you could also do this

$data = array(
   96 => array('urun' => ''),
   108 => array('urun' => ''),
   120 => array('urun' => '')
while($row = $res->fetch_assoc()) {
    $data[$row['key_']] = $row;

Open in new window

Now you are guaranteed that $data[96]['urun'] has a value.

All of the above is speculation though - I would need to know more of what you are trying to do.

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
BRMarketingAuthor Commented:
Thank you Julian Hansen
You gave a wonderful answer. Thank you.
I learned a lot from you. Best regards.
Julian HansenCommented:
You are welcome.
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

From novice to tech pro — start learning today.