Questo errore si riceve:
SQLSTATE [HY093]: numero di parametro non valido: il parametro non è stato definito
è perché il numero di elementi in $values
& $matches
non è il lo stesso o $matches
contiene più di 1 elemento.
Se $matches
contiene più di 1 elemento, che l'inserto sarà sicuro, perché c'è nome solo 1 colonna di riferimento nella query (hash
)
Se $values
& $matches
non contengono lo stesso numero di elementi allora la anche l'inserimento fallirà, a causa della query che si aspetta x parametri, ma sta ricevendo y dati $matches
.
Credo che sarà anche necessario assicurarsi che l'hash della colonna abbia anche un indice univoco.
provare il codice here:
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = '';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=test", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$matches = array('1');
$count = count($matches);
for($i = 0; $i < $count; ++$i) {
$values[] = '?';
}
// INSERT INTO DATABASE
$sql = "INSERT INTO hashes (hash) VALUES (" . implode(', ', $values) . ") ON DUPLICATE KEY UPDATE hash='hash'";
$stmt = $dbh->prepare($sql);
$data = $stmt->execute($matches);
//Error reporting if something went wrong...
var_dump($dbh->errorInfo());
?>
Sarà necessario adattarlo un po '.
struttura tabella che ho usato è here:
CREATE TABLE IF NOT EXISTS `hashes` (
`hashid` int(11) NOT NULL AUTO_INCREMENT,
`hash` varchar(250) NOT NULL,
PRIMARY KEY (`hashid`),
UNIQUE KEY `hash1` (`hash`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
codice è stato eseguito sul mio server XAMPP che sta usando PHP 5.3.8 e MySQL 5.5.16.
Spero che questo aiuti.
Hai provato a passare array_values ($ corrisponde)? –
Perché "ON DUPLICATE KEY UPDATE hash = hash'? Può anche fare "INSERIRE IGNORA ...". Intendevi fare 'UPDATE hash = VALUES (hash)'? – eggyal
La matrice '$ values' conteneva già qualcosa prima del ciclo? Inoltre, perché non preparare un singolo 'INSERT INTO hash (hash) VALUES (?)' Ed eseguirlo più volte? – eggyal