简体   繁体   中英

How can I bind an argument to mysqli query?

I wrote the following code:

$date = new DateTime();
$xxxxx= $date->getTimestamp(); 

if ($result = $mysqli->query("SELECT text_content, text_duration, start_time from texts WHERE start_time > xxxxx order by start_time asc")) {

while($row = $result->fetch_array(MYSQL_ASSOC)) {
            $myArray[] = $row;
    }
    echo json_encode($myArray);
}

and now - how can I substitute the xxxxx with the starttime in the most efficient way?

Please take a look into this: http://php.net/manual/en/mysqli-stmt.bind-param.php

$date = new DateTime();
$xxxxx = $date->getTimestamp();

$sql = "SELECT text_content, text_duration, start_time from texts WHERE start_time > ? order by start_time asc";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $xxxxx);
$stmt->execute();
$result = $stmt->execute();

Then use json_encode with your result:

echo json_encode($result);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 

$ts   = time();
$sql  = "SELECT text_content, text_duration, start_time from texts 
         WHERE start_time > ? order by start_time asc";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $ts);
$stmt->execute();
$stmt->get_result()->fetch_all(MYSQLI_ASSOC);
echo json_encode($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM