Search code examples
phpmysqlitimestampbindparam

PHP bindParam with mysql function


Is it possible to use OOP of PHP where I could bind_param with mysql build-in functions?

I have a following code which gives me an error of:

Fatal error:  Call to undefined function FROM_UNIXTIME() in...

And the code is:

$sql = $connection->prepare("INSERT INTO table_name (value1, value2, value3)
                             VALUES (?, ?, ?)");

$sql->bind_param('sss', $value1, $value2, FROM_UNIXTIME(" . $value3) . "));
$db -> query($sql, $connection);

As far as I understand from the error PHP does not offer FROM_UNIXTIME() method therefore my problem is that bind_param compiles string from my query into some-kin-of code which then is sent to mysql db. As where I could sent FROM_UNIXTIME as a string in a query then mysql would understand that method and act accordingly.

Anyway how can I run such a query with bind_param? I tried to convert my date-time to timestamp and put the output into the query but mysql shows only zeros in the table.


Solution

  • If you want to use placeholders as arguments of mysql-functions - do it:

    $sql = $connection->prepare("INSERT INTO table_name (value1, value2, value3) VALUES (?, ?, FROM_UNIXTIME(?))");
    $sql->bind_param('sss', $value1, $value2, $value3);