mysql - How to use a php variable? -


i have such dilema , headache, i'm working in php , mysql using dates.

in workbench , phpmyadmin works fine , returns me rows

select * reserva str_to_date( '2017-04-07 19:00:00' , '%y-%m-%d %h:%i:%s') between fechaini , fechafin 

where fechafin , fechaini datetime fields, in php insert, works fine, , i'm filling db this.

public function storereserva($fechaini) {       $uuid = uniqid('', true);      $stmt = $this->conn->prepare("insert reserva(unique_id, fechaini, fechafin) values(?, ? , ? + interval 2 hour)");     $stmt->bind_param("sss", $uuid, $fechaini, $fechaini);     $result = $stmt->execute();     $stmt->close();      // check successful store     if ($result) {         $stmt = $this->conn->prepare("select * reserva unique_id = ?");         $stmt->bind_param("s", $uuid);         $stmt->execute();         $reserva = $stmt->get_result()->fetch_assoc();         $stmt->close();          return $reserva;      } else {          return false;     } } 

i want verify if new fechaini isnt between in row of fields

public function storereserva2($fechaini) {      $stmt1 = $this->conn->prepare("select * reserva str_to_date( '.$fechaini' , '%y-%m-%d %h:%i:%s') between fechaini , fechafin");     $result1 = $stmt1->execute();     $stmt1->close();     if ($result1 !== null)     {          $uuid = uniqid('', true);         $stmt = $this->conn->prepare("insert reserva(unique_id, fechaini, fechafin) values(?, ? , ? + interval 2 hour)");         $stmt->bind_param("sss", $uuid, $fechaini, $fechaini);         $result = $stmt->execute();         $stmt->close();          // revisa si se hizo la reserva con su id unica         if ($result) {             $stmt3 =  $this->conn->prepare("select * reserva unique_id = ?");             $stmt3->bind_param("s", $uuid);             $stmt3->execute();             $reserva = $stmt3->get_result()->fetch_assoc();             $stmt3->close();              return $reserva;          } 

the statement doesnt work, returns diferent null whatever dates are. i'm guessing i'm using php variable $fechaini wrong if can me correct syntax.

pd: tested , without dot ($fechaini) i'm not kinda dumb (i can't see sentente return, i'm using android studio... there way see php echo or print? i'm not advanced not intermediate php user. )

well, solved this, using mysqli, works, managing wrong result, using num of rows, wanted. everyone. hope helps how manage return result of query.

public function storereserva2($fechaini) {     $servername = "localhost"; $username = "root"; $password = ""; $dbname = "android_api";  // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); }   $sql = "select * reserva str_to_date( '$fechaini' , '%y-%m-%d %h:%i:%s') between fechaini , fechafin"; $result = $conn->query($sql);  if ($result->num_rows > 0) {    echo "fechas existentes";    return false;  } else {       $uuid = uniqid('', true);             $stmt = $this->conn->prepare("insert reserva(unique_id, fechaini, fechafin) values(?, ? , ? + interval 2 hour)");             $stmt->bind_param("sss", $uuid, $fechaini, $fechaini);             $result = $stmt->execute();             $stmt->close();              // revisa si se hizo la reserva con su id unica             if ($result) {                 $stmt3 =  $this->conn->prepare("select * reserva unique_id = ?");                 $stmt3->bind_param("s", $uuid);                 $stmt3->execute();                 $reserva = $stmt3->get_result()->fetch_assoc();                 $stmt3->close();                  return $reserva;              }  } $conn->close();         } 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -