waterdripz waterdripz - 2 years ago 58
MySQL Question

Fetched values from MIN(CAST(.. AS DATETIME)) output to php error

I have this little problem of fetching the results via a PDO query:

$result = $db->prepare("SELECT MIN( CAST( Tstart AS DATETIME ) ) , MAX( CAST( Tend AS DATETIME ) ) , FROM TimeBase
WHERE Daynr = '131'");
$keep = $result->fetch();

echo $keep['Tstart'];
echo $keep['Tend'];

Produces errors:

Undefined index: Tstart

Undefined index: Tend

The Mysql syntax itself returns a result when I run it directly.
And the PDO was not causing any problems without the
so I'm sure the problem has to do something with this syntax, but I can't find the correct way of fetching it to a php variable (or echo in this example).

So my question is what is the correct way of apprehending the fetched row as a php variable in this case?

From there will I be able to use this code to format it as time from a datetime row?

$Start_Time = $keep['time(Tstart)'];

Thank you in advance,

Answer Source

You could either treat the result as a positional array:

echo $keep[0];
echo $keep[1];

or give your columns aliases and treat the result as an associative array:

$result = $db->prepare("SELECT MIN( CAST( Tstart AS DATETIME ) ) AS tstart, 
FROM TimeBase
WHERE Daynr = '131'");
$keep = $result->fetch();

echo $keep['tstart'];
echo $keep['tend'];

Note, BTW, that you have a redundant comma before your from clause.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download