0
I’m trying to do a query via php in Mysql , but it’s not working in PHP, but it works in PHPMYADMIN.
$check_in = strtotime($arrival);
$check_out = strtotime($departure);
Queries I’ve tried in PHP...
$query = "SELECT * FROM wp_st_availability WHERE post_id = 5993 AND check_in > '$check_in' and check_in < '$check_out'";
$query = "SELECT * FROM wp_st_availability WHERE post_id = '5993' AND check_in > '$check_in' and check_in < '$check_out'";
$query = "SELECT * FROM wp_st_availability WHERE post_id = '5993' AND check_in > '" . $check_in . "' and check_in < '" . $check_out . "'
;
Full code: $query = "SELECT * FROM wp_st_availability WHERE post_id = '$post_id' AND check_in > '" . $check_in . " ' and check_in < '" . $check_out . " "; echo nl2br($query); $query = $this->db->query($query); if ($query->rowCount() == 1) { echo "found"; Exit; } Else { echo "found nothing"; Exit; }
New code, I’m using PDO. Sorry for formatting, I can’t format for code.
$check_in = '1514764800'; $check_out = '1525132800';
$post_id = "5993";
$query = ("SELECT * FROM wp_st_availability WHERE post_id='$post_id' AND check_in > '$check_in' AND check_in < '$check_out'");
// $query = ("SELECT * FROM wp_st_availability WHERE post_id = '$post_id' AND check_in > '" . 1514764800 . "' and check_in < '" . 1525132800 . "'");
$query = $this->db->query($query);
if ($query->rowCount() == 1) {
echo "achou";
exit;
} else {
echo "nao achou nada";
exit;
}
The first version is correct. Explain better what is "not working in php".
– bfavaretto
Make a
echo nl2br($query)
to see the returns– William Aparecido Brandino
echo return nl2br($query) = SELECT * FROM wp_st_availability WHERE post_id = '5993' AND check_in > '1514764800' and check_in < '1525132800'
– Rafael Saru
@bfavaretto it executes ok in phpmyadmin ( returns results ) and in php it returns nothing in rowCount
– Rafael Saru
What is the $Rrival and $departure format? Is strtotime working? If you are, do it
date('Y-m-d, $strtotime(...))
.– bfavaretto
The strtotime ta working yes.. $check_in comes 1514764800 and $check_out 1525132800
– Rafael Saru
The problem is not the format, because when I search in phpmyadmin appears the values. I took the variables and put the value of check_in and check_out and returned nothing in php. I did the same thing in phpmyadmin and returned the results
– Rafael Saru
You may be making an error in the connection or in the query itself. I don’t know what you use to access mysql (mysqli? Pdo?), but you need to call the function that informs you errors.
– bfavaretto