Store subquery in a variable (PHP)

0

Can I store the result of a query in a variable and use the value in another query?

//consulta que quiero hacer primero
$consulta_pre_has = "SELECT
                         MAX(Prod_Pres) 
                     FROM productos";

//hago la consulta
$pre_has=mysqli_query($conexion, $consulta_pre_has);

I use the variable $ pre_has that theoretically stores the value of the query in another query

SELECT 
    * 
FROM 
    productos 
WHERE  
    Prod_Pres 
BETWEEN 
    0 AND '$pre_has'

The problem is that if I want to show the value of $ pre_has on the screen, it does not show me anything and therefore I understand that at the time of making the previous query the value of $ pre_has is null and that's why it does not work for me

    
asked by gmarsi 23.03.2017 в 13:01
source

1 answer

4

You are storing something that is not really the result of your query, but a MySQLi response object

Instead of:

$consulta_pre_has = "SELECT MAX(Prod_Pres) FROM productos";  

$pre_has = mysqli_query($conexion, $consulta_pre_has);

It should be:

$consulta_pre_has = "SELECT MAX(Prod_Pres) as pre_has FROM productos";  
$result = mysqli_query($conexion, $consulta_pre_has);
$row = mysqli_fetch_assoc($result);

$pre_has = $row['pre_has'];
    
answered by 23.03.2017 в 13:24