Jump to content

[SOLVED] query returns empty w/PHP, works w/ PHPMyAdmin


JoeMajesty

Recommended Posts

Hi all,

 

I'm trying to get a query working.  My Apache server running on Redhat uses PHPMyAdmin 2.8.2.4  to administer MySQL 3.23.58.  PHP version is 4.3.2.

 

If I run the query below in PHPMyAdmin it returns the correct result.  If I run the query on my webpage, I get "Query was empty".  I'm pretty new at PHP/MySQL and programming in general, but all my other queries work.  This is the first time I've tried to use the BETWEEN keyword in a query.

 

//end pconnect.inc
$thispdstart = '2007-06-04';
$thispdend = '2007-06-17';
$sql1 = 'SELECT * FROM `HrsWorked` WHERE `EmployeeNumber` = 18 AND `Date` between \'2007-06-04\' and \'2007-06-17\' ORDER BY `Date`';
'$thispdstart' and '$thispdend' ORDER BY 'Date'";
echo "$sql1<br>";
    $sql1 = mysql_query($qry1, $linkID) or die (mysql_error());

 

Here is what I've tried:

As you can see, my webpage echos the query first, then executes.  The output is "query is empty".  However if I copy and paste the echoed query into the SQL function in PHPMyAdmin it returns the correct results.  If I use the "create PHP code" functionality from PHPMyAdmin it yields the following query and the same result on my webpage.

 

$sql = 'SELECT * FROM `HrsWorked` WHERE `EmployeeNumber` = 18 AND `Date` between \'2007-06-04\' and \'2007-06-17\' ORDER BY `Date`';

 

Thanks...

<?php

//end pconnect.inc
$thispdstart = "2007-06-04";
$thispdend = "2007-06-17";
$sql1 = "SELECT * FROM HrsWorked WHERE EmployeeNumber = 18 AND Date BETWEEN '{$thispdstart}' AND '{$thispdend}' ORDER BY Date";

echo "{$sql1}<br />";
$sql1 = mysql_query($sql1, $linkID) or die (mysql_error());

?>

 

Neatened it up a little, and made the query use your variables :)

Yikes, noticed a problem myself, but not a fix...

 

There is the variable storing the query and the query results is the same.  That explains "query is empty".

 

Fixed, the code is now:

 

$qry1 = 'SELECT * FROM `HrsWorked` WHERE `EmployeeNumber` = 18 AND `Date` between \'2007-06-04\' and \'2007-06-17\' ORDER BY `Date`';
echo "$qry1<br>";
$sql1 = mysql_query($qry1, $linkID) or die (mysql_error());
while ($row = mysql_fetch_array($sql1))
     {
     echo $sql1['Date']."<br>";
     }

 

The only output from this page is the query.  I'm not using the variables because I want fewer, well, variables until I solve this problem.

 

I tried chigley's query and get the same result.

 

 

<?php

//end pconnect.inc
$thispdstart = "2007-06-04";
$thispdend = "2007-06-17";
$query = "SELECT * FROM HrsWorked WHERE EmployeeNumber = 18 AND Date BETWEEN '{$thispdstart}' AND '{$thispdend}' ORDER BY Date";

echo "{$query}<br />";
$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_assoc($result);

echo "<pre>".print_r($row, true)."</pre>";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.