Is this possible? e.g.
SELECT * FROM :database WHERE id = :id
If not, should I just do this:
SELECT * FROM ' . $database . ' WHERE id = :id
Or is there some other trick I need to learn?
Is this possible? e.g.
SELECT * FROM :database WHERE id = :id
If not, should I just do this:
SELECT * FROM ' . $database . ' WHERE id = :id
Or is there some other trick I need to learn?
The following works for me:
<?php
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "pass");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stmt = $pdo->prepare("INSERT INTO `null_test` (`can_be_null`) VALUES (:null)");
$stmt->bindValue(":null", null, PDO::PARAM_NULL);
$stmt->execute();
Pass in PHP's null
, with type of PDO::PARAM_NULL
. Also, make sure your prepare emulation is set to false. That might help.
Not near a terminal to check, but I believe you have to type bind it to INT and send it in as an INT, not as "b010101" (or whatever):
$sql='INSERT INTO test(id,data) VALUES(:id,:bit)';
$stmt=db::db()->prepare($sql);
$stmt->bindValue('id', null, PDO::PARAM_NULL);
$stmt->bindValue('bit', (int)$value, PDO::PARAM_INT);
$stmt->execute();
Quick check on Google brought up this similar previous answer.
lastInsertId() is a method of the PDO class, not the PDOStatement class.
This should work:
$groupID = $dbo->lastInsertId();
No, you cannot bind identifiers, only values.
Identifiers (table names, field names, etc.) are not supposed to be user inputs in the first place. It is a very bad idea to handle them in such a way.
Table and Column names cannot be replaced by parameters in PDO. see Can PHP PDO Statements accept the table or column name as parameter?