-2

For some reason i can not get php/mysql to do JOIN with a between option. Am i over looking something again?

When i run this script all i get is a whit page with no errors on it....?

Any ideas would help!

 $start = date("m/1/Y");
 $end = date("m/31/Y");

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

$output = fopen('php://output', 'w');

fputcsv($output, array('ID', 'P.O #', 'Date', 'To', 'Time', 'Vin', 'Reason', 'Amount', 'Employee #', 'Mananger', 'Account #', 'Store Location', 'Borrowed', 'verified','Adjustment','Adjustment by','','VOID','Emp Name'));

mysql_connect('*********', '**********', '***********');
mysql_select_db('donavons_car_scanner');
$rows = mysql_query('SELECT fpo.*, users.name FROM fpo  WHERE date BETWEEN '" . $start . "' AND  '" . $end . "' LEFT OUTER JOIN users ON (fpo.empnum = users.empnum)');

while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);

Thank you!

Kelly Hansen
  • 69
  • 10

2 Answers2

1

You've put the JOIN in the wrong place, you want to join tables before adding conditions -

SELECT fpo.*, users.name 
FROM fpo 
LEFT OUTER JOIN users 
ON (fpo.empnum = users.empnum)' 
WHERE date BETWEEN '" . $start . "' AND  '" . $end . "' 

You should really stop using mysql_* functions.** They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and **use PDO.

Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
1

In addition to Jay's answer (since I can't comment) the mysql date is expected in yyyy-mm-dd.

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

-http://dev.mysql.com/doc/refman/5.1/en/datetime.html

$start = date("m/1/Y");
$end = date("m/31/Y");

should be

$start = date("Y/m/1");
$end = date("Y/m/31");
user3783243
  • 5,368
  • 5
  • 22
  • 41