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
LVL 1
BRMarketingAsked:
Who is Participating?
 
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.
2
 
arnoldCommented:
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.

Y
0
 
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?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
arnoldCommented:
I do not understand what you are after,
Your assignment step seems unnecessary.
I would check if the key is


Case . $row['_key'] i
0
 
BRMarketingAuthor Commented:
Thank you Julian Hansen
You gave a wonderful answer. Thank you.
I learned a lot from you. Best regards.
0
 
Julian HansenCommented:
You are welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.