Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

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
Avatar of arnold
arnold
Flag of United States of America image

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
Avatar of BR

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

Thank you Julian Hansen
You gave a wonderful answer. Thank you.
I learned a lot from you. Best regards.
You are welcome.