0

I am trying to work through an SSE push example, but am running into what I believe is a problem with myselect statement.

$stm = $db->prepare("SELECT id, message FROM messages WHERE id > :id");

The example I'm following was originally written for SQlite3, so I wonder if there is some syntax variation that I'm dealing with here?

The specific error from mysql is:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':id LIMIT 0, 25' at line 1

If it matters: My table named messages has two columns: id and message. The id column is data type INT, and is set to auto-increment new rows. The message column is data type VARCHAR with a limit of 255.

The php returns the following error at this line, which I'm fairly certain is due to the select statement problem:

$stm->bindValue('id', $id);

Uncaught Error: Call to a member function bindValue() on boolean

The entire php code is:

<?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);
}
?>
Dshiz
  • 3,099
  • 3
  • 26
  • 53
  • If this question is a duplicate of http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php, could you explain where in my code this duplicate question/answer applies? I am using a single mysqli_ connection statement and to my knowledge I am not mixing MySQL API's! – Dshiz Apr 01 '17 at 22:45
  • 1
    yes, you are mixing mysqli with pdo. – Shadow Apr 01 '17 at 22:47
  • 1
    `$stm->bindValue` is PDO. – chris85 Apr 01 '17 at 22:47
  • Thank you guys! This is what I needed – Dshiz Apr 01 '17 at 22:47
  • Btw, mysqli does not support named parameters. – Shadow Apr 01 '17 at 22:48
  • I was able to get past the initial error by creating a PDO connection, but I'm now running into a fatal exception on, fetchArray() ... Am I still mixing APIs somewhere? – Dshiz Apr 02 '17 at 00:14

0 Answers0