I have this code below:
if(isset($_POST['period'])){
$query_str = "SELECT `id` FROM `orders` WHERE `status` = 'Send' AND `date` BETWEEN '".date('Y-m-d H:i:s', time()-$_POST['period'])."' AND '".date('Y-m-d H:i:s', time())."' ";
echo $query_str;
$query = mysql_query($query_str) or die(mysql_error());
$arr = mysql_fetch_assoc($query);
var_dump($arr);
}
As result I have these two strings:
// this value of $query_str
SELECT `id` FROM `orders` WHERE `status` = 'Send' AND `date` BETWEEN '2015-06-04 03:11:08' AND '2015-06-11 03:11:08'
// var_dump of my $arr
array(1) {
["id"]=>
string(2) "25"
}
This result is wrong, but when I am copying $uqery_str
value and put it into phpMyAdmin I have 3 results.
25,26,27 // this is right
Can anybody explain what is wrong?
That is because you only fetch the first row. Put your fetch call into a while loop to fetch all rows, e.g.
to:
$query
is a resource and withmysql_fetch_assoc()
you fetch row by row from the resource. As from the manual:Also since we are here I give you a good night story for today:
mysql_*
is deprecated and is removed in new versions of PHP. So I would highly recommend you to look intoPDO
ormysqli_* prepared statements
.