kubaszy
kubaszy

Reputation: 23

Problem with get single value from db mysql

I've a problem with get a single value from mysql and variable assignment. var_dump($val) looks ok:

object(stdClass)#5 (1) { ["min(ID_plants)"]=> string(1) "1" }

And i need this "1" assign to variable.

$first= "SELECT min(ID_plants) from Plants";
if (mysqli_query($link,$first)){
    $res = mysqli_query($link,$first);
    $val = mysqli_fetch_object($res);
}
//var_dump($val);
//$first_index$=($val->MIN(ID_plants));


Upvotes: 0

Views: 33

Answers (2)

Rakesh Jakhar
Rakesh Jakhar

Reputation: 6388

You should use prepared statement instead of mysqli.

Change your query to

 SELECT min(ID_plants) As minPlants from Plants // Alias

After $val = mysqli_fetch_object($res); you can get the value by using $val->minPlants

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133400

you should use an alias for min(ID_plants)

$first= "SELECT min(ID_plants) my_min_id from Plants";
 if (mysqli_query($link,$first)){
     $res = mysqli_query($link,$first);
     $val = mysqli_fetch_object($res);
 }
 //var_dump($val);
 $first_index$=($val->my_min_id);

Upvotes: 2

Related Questions