Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How do I execute dynamic sql (prepared statements)?
  • When I try to execute the following code, I get an error Fuel\Core\Database_Exception [ 1064 ]: You have an error in your SQL syntax;

    PHP with MySQL embeded

    $sql = "
    SET @sql = CONCAT(
    'CREATE TEMPORARY TABLE IF NOT EXISTS temp_compiled_properties AS
    (SELECT
    id,
    ', @sql, '
    FROM properties
    WHERE properties.property_id = ?
    GROUP BY properties.property_id)');

    PREPARE stmt FROM @sql;
    EXECUTE stmt USING @property_id;
    DEALLOCATE PREPARE stmt;";

    DB::query($sql)->execute();
  • HarroHarro
    Accepted Answer
    If it gives you that error, it will also tell you which part it thinks has the error?

    DB::query() just passes the code for execution without any modification.

    I assume the problem is that this string contains multiple SQL commands, the query() method of the selected driver (mysql, mysqli, pdo) doesn't support that. See for example http://php.net/manual/en/mysqli.quickstart.multiple-statement.php

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion