¿Cómo se utilizan declaraciones preparadas de PDO con una columna BIT(1)?
Frecuentes
Visto 888 equipos
1
I have a database table with a few BIT(1)
type columns. If I were to forget about prepared statements, I could easily do something like this:
UPDATE tablename SET
bit_column_1 = b'1',
bit_column_2 = b'0'
And this would work perfectly. However, no matter what I try, when using prepared statements, the value is always '1'.
I have done the following and none of them work if $_POST['bit_col']
is 0
:
$stmt = $dbh->prepare("UPDATE tablename SET
bit_col = :bit_col ");
// First attempt
$stmt->bindValue('bit_col', $_POST['bit_col']);
// Second attempt
$stmt->bindValue('bit_col', $_POST['bit_col'], PDO::PARAM_INT);
// Third attempt
$stmt->bindValue('bit_col', "b'{$_POST['bit_col']}'");
Then I tried altering the prepared statement to put the b
there, but I get number of bound variables does not match number of tokens
$stmt = $dbh->prepare("UPDATE tablename SET
bit_col = b:bit_col ");
$stmt->bindValue('bit_col', $_POST['bit_col']);
$stmt = $dbh->prepare("UPDATE tablename SET
bit_col = b':bit_col' ");
$stmt->bindValue('bit_col', $_POST['bit_col']);
Otra cosa que vale la pena mencionar es PDO::ATTR_EMULATE_PREPARES
se establece a true
. Estableciendo esto en false
would require me to refactor things quite a bit because of how I unknowingly managed database connections.
So my question is, is it possible to use prepared statements with BIT
columns in MySQL, and if so, how?
2 Respuestas
2
PDO::ATTR_EMULATE_PREPARES
causes PDO to interact with BIT columns slightly differently. If it is set to false
, you just insert the value as normal and MySQL will do any conversion necessary behind the scenes:
$stmt = $dbh->prepare("UPDATE tablename SET
bit_col = ? ");
$stmt->bindValue(1, $_POST['bit_col']);
However, if PDO is emulating the prepared statements, you need to put a b
in there somehow to indicate that it is a BIT type. If you put the b
in the bound parameter, PDO will escape the single quotes and you will end up with something like 'b\'0\''
being sent to MySQL, which obviously won't work. The b
therefore needs to be in the query, not in the bound parameter. Doing this with named parameters produces the "number of bound variables does not match number of tokens" error above because PDO does not recognize strings with a b
seguido por un :
as a named parameter. However PDO sí recognize it as a parameter when you use question mark parameter markers, like this:
$stmt = $dbh->prepare("UPDATE tablename SET
bit_col = b? ");
$stmt->bindValue(1, $_POST['bit_col']);
Since we need to end up with something like b'1'
being sent to MySQL, using PDO::PARAM_INT
when binding the value will cause the query to fail because it will become UPDATE tablename SET bit_col = b1
(without the quotes around the number) so you must leave the data type out or use PDO::PARAM_STR
.
Also note that if emulating prepared statements is disabled, this query will fail with a syntax error, so unfortunately the queries need to be done completely differently depending on whether or not you are emulating prepares or not.
Respondido 13 Feb 14, 16:02
0
if I'm not mistaken, the syntax should be:
$stmt->bindValue(':bit_col', $_POST['bit_col']);
to reference the :bit_col from:
$stmt = $dbh->prepare("UPDATE tablename SET bit_col = :bit_col ");
Respondido 12 Feb 14, 06:02
Esto podría funcionar si PDO::ATTR_EMULATE_PREPARES
se establece a false
, but that was the first thing I tried, which I mentioned in my question. - Mike
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas php mysql pdo prepared-statement bit-manipulation or haz tu propia pregunta.
you could try this without parameter<br> visit stackoverflow.com/questions/10540483/… - saeed