0

Sir I have these codes

$last_date="";
$last_vno="";

$queryx = "SELECT MAX(t1.date)as mxdate,MAX(t1.vou_no)as mxvno,max(d1)as d1
FROM vouchers t1
WHERE t1.date = (SELECT MAX(t2.date)as date FROM vouchers t2 where vou_type='CP' and dr_amount>0)";

$resultx=sqlsrv_query($con,$queryx)or  die("Error". sqlsrv_errors($con)) ;
if ($resultx){
    while ($rowx = sqlsrv_fetch_array ($resultx)){
    $last_date=$rowx['mxdate'];
    $last_vno=$rowx['mxvno'];
    $myparty=$rowx['d1'];
    }
}else{
    echo "no data found";
    exit;
} 

The query work fine, no issue

nOW i want to use $last_date in an other qeury like this

$query = "SELECT * FROM vouchers where vou_type = 'CP' and date='".$last_date."' and vou_no='".$last_vno."'";

but it says:

Catchable fatal error: Object of class DateTime could not be converted to string in ...

Please help

3
Contributors
2
Replies
17
Views
3 Months
Discussion Span
Last Post by diafol
0
$query = "SELECT * FROM vouchers where vou_type = 'CP' and date='".$last_date->format('Y-m-d H:i:s')."' and vou_no='".$last_vno."'";

This should sort it, but I've found in my experience that the easiest way to manage dates and timezones in a database with php and javascript is as integers - INT(11) - unix timestamps. Use moment.js for conversion and display, and store everything as UTC.

1

I'm assuming this first query is returning ONE row, so not sure why using a while loop. Also this is just quote hell:

$query = "SELECT * FROM vouchers where vou_type = 'CP' and date='".$last_date."' and vou_no='".$last_vno."'";

Try:

$query = "SELECT * FROM vouchers where vou_type = 'CP' and date = '$last_date' and vou_no = '$last_vno'";

Is date a reserved word in SQLSRV? If so, use...

$query = "SELECT * FROM vouchers where vou_type = 'CP' and `date` = '$last_date' and vou_no = '$last_vno'";
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.