Forum Moderators: coopster

Message Too Old, No Replies

Problem with query in php mysql?

         

shams

2:32 am on Dec 5, 2006 (gmt 0)

10+ Year Member



hi,
this is a mysql query shows the rows of table pharmacy with columns id,Code,Name ,Exp (the drug expiry date)and ExpLeft (Expiray Left for the drugs in months) for the drugs, the query is working fine without (WHERE ExpLeft='$ExpLef'), i want to query for the column ExpLeft for exmple for the drugs EcpLeft is 12 months, the code is not working with the query WHERE , there is no any error in ouput just shows the table headers.

<html>
<head>
<title>Open Pharmacy</title>
<style type="text/css">
table tr td { font: 12px Verdana, Arial, sans-serif;
text-align: center;
font-weight: bold;
}
table tr th { font-size: 12px;
text-align: center;
}
</style>
</head>

<body>

<?php
include 'library/config.php';
include 'library/opendb.php';
include 'library/paging_function.php';
$ExpLef = $_POST['EexpLef'];
// query the table
$result = mysql_query("SELECT count(*) FROM pharmacy");
$total = mysql_result($result, 0);
$news = mysql_query("SELECT * FROM pharmacy WHERE ExpLeft='ExpLef' ORDER BY id ASC LIMIT $start, $display");
echo "<table border='1'>";
echo "<tr><th>Id</th><th>Code</th><th>Name</th><th>ExpLeft</th></tr>";
while($row = mysql_fetch_assoc($news)) {
// Expiray Left in Months
$Exp = $row[Exp];
$expires=strtotime($Exp);
$rightnow=time();
$secondsleft=$expires-$rightnow;
$ExpLeft=round($secondsleft/2592000);

echo "<tr><td>";
echo $row[id];
echo "</td><td>";
echo $row[Code];
echo "</td><td>";
echo $row[Name];
echo "</td><td>";
echo $ExpLeft;
echo "</td></tr>";
}
echo "</table>";
paginate($display, $pg, $total);
include 'library/closedb.php';
?>
</body>
</html>

supermanjnk

3:48 am on Dec 5, 2006 (gmt 0)

10+ Year Member



if this is an exact copy of your code

$news = mysql_query("SELECT * FROM pharmacy WHERE ExpLeft='ExpLef' ORDER BY id ASC LIMIT $start, $display");

shouldn't ExpLef be $ExpLef

shams

9:53 am on Dec 5, 2006 (gmt 0)

10+ Year Member



thanks for reply it was just mistake in post the real cod is:
ExpLef = $_POST['ExpLef'];
ExpLeft = '$ExpLef'

mcibor

10:07 am on Dec 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think equals (=) is a bad operator for expiry left.

ExpLeft should be in DATE format, and you should store there expiry date, eg. 2007-01-23 then
let's say you want 12 months expiry still:

$ExpLef = (int)$_POST['EexpLef'];// = 12 (in months) int to force any string, etc into a number

Then the query could be
SELECT * FROM `pharmacy` WHERE `ExpLeft` >= DATE_SUB(CURDATE(),INTERVAL $EcpLef MONTH) ORDER BY id ASC LIMIT $start, $display;

this will return all that have expiry date above 2007-12-05

Hope this helps
Michal