Desidero aggiornare il mio codice corrente che viene costantemente iniettato in sql con PDO.Come aggiungere valori variabili all'interno di pdo-> query
Attualmente sono bloccato con l'utilizzo di una variabile all'interno di una query PDO.
Se ho due argomenti come questo
$rowsPerPage = 3;
// by default we show first page
$pageNum = 1;
if (isset($_GET['page'])) {
$pageNum = mysql_real_escape_string($_GET['page']);
}
$offset = ($pageNum - 1) * $rowsPerPage;
E devo query come questa
$STH = $DBH->query("SELECT News.ID, LEFT(NewsText,650), Title, AID, Date, imgID," .
"DATE_FORMAT(Date, '%d.%m.%Y.') as formated_date " .
"FROM News, Categories, NewsCheck WHERE Name LIKE '%News - Block%' AND CID=Categories.ID AND JID=News.ID ".
"ORDER BY `Date` DESC LIMIT $offset, $rowsPerPage");
DOP segnala un errore nell'ultima riga della ORDER query Se si sostituisce questi linea con "ORDER BY Date DESC LIMIT3,3");
tutto funziona.
Quindi, come aggiungere valori variabili all'interno di PDO :: query?
Aggiornato: Grazie per rispondere a soffietto Ho aggiornato il mio codice come questo
$STH = $DBH->prepare("SELECT News.ID, LEFT(NewsText,650), Title, AID, Date, imgID," .
"DATE_FORMAT(Date, '%d.%m.%Y.') as formated_date " .
"FROM News, Categories, NewsCheck WHERE Name LIKE '%News - Block%' AND CID=Categories.ID AND JID=News.ID ".
"ORDER BY `Date` DESC LIMIT :offset, :rowsPerPage;");
$STH->bindParam(':offset', $offset, PDO::PARAM_STR);
$STH->bindParam(':rowsPerPage', $rowsPerPage, PDO::PARAM_STR);
$STH->execute();
Ma Errore:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''-3', '3'' at line 1' in /pdo/test.php:42 Stack trace: #0 /pdo/test.php(42): PDOStatement->execute() #1 {main} thrown in /pdo/test..
secondo aggiornamento Cambiato da PARAM_STR PER PARAM_INT come questo
$STH->bindParam(':offset', $offset, PDO::PARAM_INT);
$STH->bindParam(':rowsPerPage', $rowsPerPage, PDO::PARAM_INT);
Tutto funziona.
assegnare la stringa di query a una variabile, var_dump() i risultati e postare qui per favore. –