0

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));


1
  • use alias to fetch min(ID_plants), that would might help. Like this $first= "SELECT min(ID_plants) as minid from Plants"; /** rest of the code */ $first_index = $val->minid; echo $first_index; Commented Sep 4, 2019 at 8:12

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.