我正在尝试通过一个SSE推送示例,但我遇到了我认为myselect语句的问题。
$stm = $db->prepare("SELECT id, message FROM messages WHERE id > :id");
我跟随的示例最初是为SQlite3编写的,所以我想知道我在这里处理的语法变化是否存在?
mysql的具体错误是:
#1064 - 您的SQL语法出错;检查手册 对应于您的MariaDB服务器版本,以获得正确的语法 靠近':id LIMIT 0,25'在第1行
如果重要:我的名为messages
的表格有两列:id
和message
。 id
列是数据类型INT,并设置为自动递增新行。消息列是数据类型VARCHAR,限制为255。
php在这一行返回以下错误,我相当肯定是由于select语句问题:
$stm->bindValue('id', $id);
未捕获错误:在布尔值
上调用成员函数bindValue()
整个php代码是:
<?php
/*
Create table as:-
create table data (id integer primary key, message varchar)
Then to see the update on browser, insert new message:-
insert into data (message) values ('hello');
...
insert into data (message) values ('second');
*/
$db = mysqli_connect('localhost', '****', '****','my_db');
//$db = new SQLite3('data.db');
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.
function send_msg($id, $msg) {
echo "id: $id" . PHP_EOL;
echo "data: $msg" . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
}
// in the event of client reconnect, it will send Last-Event-ID in the headers
// this only evaluated during the first request and subsequent reconnect from client
$last_event_id = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : False);
if ($last_event_id == 0) {
$last_event_id = floatval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : False);
}
// also keep our own last id for normal updates but favor last_event_id if it exists
// since on each reconnect, this value will lost
$last_id = 0;
while (1) {
error_log('$last_id:' . $last_id, 4);
error_log('$last_event_id:' . $last_event_id, 4);
$id = $last_event_id != False ? $last_event_id : $last_id;
$stm = $db->prepare("SELECT id, message FROM messages WHERE id > :id");
$stm->bindValue('id', $id);
$results = $stm->execute();
if ($results) {
while ($row = $results->fetchArray()) {
if ($row) {
send_msg($row['id'], $row['message']);
$last_id = $row['id'];
}
}
}
sleep(5);
}
?>