Query works in phpmyadmin, but does not work in PHP

Asked

Viewed 177 times

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;
  }

table

  • The first version is correct. Explain better what is "not working in php".

  • Make a echo nl2br($query) to see the returns

  • echo return nl2br($query) = SELECT * FROM wp_st_availability WHERE post_id = '5993' AND check_in > '1514764800' and check_in < '1525132800'

  • @bfavaretto it executes ok in phpmyadmin ( returns results ) and in php it returns nothing in rowCount

  • What is the $Rrival and $departure format? Is strtotime working? If you are, do it date('Y-m-d, $strtotime(...)).

  • The strtotime ta working yes.. $check_in comes 1514764800 and $check_out 1525132800

  • 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

  • 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.

Show 3 more comments

1 answer

0


Look, I did it like this:

On the bench: inserir a descrição da imagem aqui

In php code:

 <?php
 $mysqli = new mysqli("host", "user", "password", "db");

$id_post = 1;

$check_in = strtotime('2018-02-12') ;
$check_out = strtotime('2018-02-15');

//$check_in =1518393600;
//$check_out = 1518652800;

if($sth = $mysqli->query("SELECT * FROM teste WHERE id='$id_post' AND 
check_in > '$check_in' AND check_in < '$check_out'")){
    while ($row = $sth->fetch_assoc()) {
       echo $row['nome'];
    }
}

reply:

inserir a descrição da imagem aqui

Note that in the bank the value check_in is 1518393700, the value of the variable $check_in is less than it and the check_in is less than $check_out, that is, between the two, review in your table the values of check_in, maybe this is the problem, to be sure of the searched values, you can do a var_dump($variable); to see if variable is going right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.