convert the result of a SQL query into an entire PHP variable

0

I want to store the result of an SQL query in a PHP variable but as an integer, this is my code:

$maxnroviaje=mysql_query("SELECT MAX(NROVIAJE) from viajes");
$nroviajenew=mysql_fetch_assoc($maxnroviaje);

and doing echo on those two variables:

<?php echo $maxnroviaje; ?>
<?php echo $nroviajenew; ?>

what throws me is the following:

  

Resource id # 15

     

Notice: Array to string conversion in   C: \ xampp \ htdocs \ agent-nav-online \ add-trip.php on line 47 Array

    
asked by eduardoimm 19.02.2018 в 03:45
source

3 answers

0

I solved it in the following way:

$maxnroviaje=mysql_query("SELECT MAX(NROVIAJE) as maximo from viajes");
$nroviajenew=mysql_fetch_assoc($maxnroviaje);
$nroviajex=$nroviajenew["maximo"];

Of course, based on the answers posted here.

    
answered by 24.02.2018 / 00:29
source
0

1.- $ maxnroviaje is an arrangement because it is the information that returns mysql from the query at a general level.

2.- $ nroviajenew is also an arrangement because it is the result of the conversion of the query to the requested fields.

You could try doing this:

$maxnroviaje = $db->query("SELECT MAX(NROVIAJE) as cuenta from viajes");
$nroviajenew = $maxnroviaje->fetch_assoc();

echo $nroviajenew["cuenta"];
//otra alternativa para mostrar el resultado de un vector 
echo $nroviajenew->cuenta;

in this way if you would be showing the result you are looking for.

Greetings

  

NOTE: The mysql library is depreciated in PHP, you must use mysqli or pdo for your queries, that way you will avoid your system failing in the future.

    
answered by 19.02.2018 в 04:02
-1

$nroviajenew is an array, it is not a single value. You can use var_dump to see the structure of the array.

The correct thing in this case is to use $nroviajenew[0] .

    
answered by 19.02.2018 в 03:52