que estoy tratando de obligar a los parámetros de consulta SQL dentro de un bucle:params vinculante para la declaración DOP dentro de un bucle
$db = new PDO('mysql:dbname=test;host=localhost', 'test', '');
$stmt = $db->prepare('INSERT INTO entries VALUES (NULL, ?, ?, ?, NULL)');
$title = 'some titile';
$post = 'some text';
$date = '2010-whatever';
$reindex = array(1 => $title, $post, $date); // indexed with 1 for bindParam
foreach ($reindex as $key => $value) {
$stmt->bindParam($key, $value);
echo "$key</br>$value</br>"; //will output: 1</br>some titile</br>2</br>some text</br>3</br>2010-whatever</br>
}
El código anterior insertos en la base de datos en los 3 campos 2010-whatever
.
Ésta funciona bien:
$stmt->bindParam(1, $title);
$stmt->bindParam(2, $post);
$stmt->bindParam(3, $date);
Por lo tanto, mi pregunta es por qué el código en el bucle foreach-falla e inserta datos erróneos en los campos?
Hola Ionesomeday - He encontré con un problema similar . ¿Podrías echarme una mano con esto por favor? http://stackoverflow.com/questions/34285341/php-function-procedure-to-bind-question-marks-dynamically – usert4jju7
La pregunta es: ¿cuál es mejor? bindValue o bindParam. –
@IdanMagled Aquí creo que 'bindValue' es más intuitivo. 'bindParam' ofrece ventajas en algunas circunstancias. – lonesomeday