Ich habe fast den folgenden Code arbeiten, aber nicht vertraut mit, wie meine Liste von Enduser zu $ IDs in Array-Form wie mein Test. Der Endbenutzer würde mir eine Liste mit "3,5,7,8 ..." senden.PHP PDO Post-String-Liste IN Wo
Wie simuliere ich "$ ids = array (1,2,3)" beim Konvertieren in eine POST-Anweisung?
<?php
//1. Create a database connection
require_once('config.php');
$mysql_host = DB_HOST;
$mysql_database = DB_NAME;
$mysql_username = DB_USER;
$mysql_password = DB_PASS;
$ids = $_POST["idsvar"]; //Doesn't return values
//$ids = array(1,2,3); //This does work when used for testing purposes
$inQuery = implode(',', array_fill(0, count($ids), '?'));
try {
$conn = new PDO("mysql:host=$mysql_host; dbname=$mysql_database", $mysql_username, $mysql_password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
//2. Perform database query if Connected Successfully
$stm = $conn ->prepare(
'SELECT `schema`.`table`.`column1` AS `DiffNameA`,
`schema`.`table`.`column2` AS `DiffNameB`
FROM `schema`.`table`
WHERE id IN(' . $inQuery . ')');
foreach ($ids as $k => $id)
$stm->bindValue(($k+1), $id);
$stm->execute();
$field = $stm->fetchAll();
foreach ($field as $row) {
print $row["DiffNameA"] . "|" .$row["DiffNameB"] ."\n\r"; //extra comma so can have notes hidden area
}
$conn = null; // Disconnect
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage().'<br />';
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
?>
Vielen Dank für jede Hilfe.
================================= ==============================
Falls es jemand hilft, das ist das komplette Skript:
<?php
//1. Create a database connection
require_once('config.php');
$mysql_host = DB_HOST;
$mysql_database = DB_NAME;
$mysql_username = DB_USER;
$mysql_password = DB_PASS;
$ids = explode(',', $_POST["idsvar"]);
$inQuery = implode(',', array_fill(0, count($ids), '?'));
try {
$conn = new PDO("mysql:host=$mysql_host; dbname=$mysql_database", $mysql_username, $mysql_password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
//2. Perform database query if Connected Successfully
$stm = $conn ->prepare(
'SELECT `schema`.`table`.`column1` AS `DiffNameA`,
`schema`.`table`.`column2` AS `DiffNameB`
FROM `schema`.`table`
WHERE id IN(' . $inQuery . ')');
foreach ($ids as $k => $id)
$stm->bindValue(($k+1), $id);
$stm->execute();
$field = $stm->fetchAll();
foreach ($field as $row) {
print $row["DiffNameA"] . "|" .$row["DiffNameB"] ."\n\r"; //extra comma so can have notes hidden area
}
$conn = null; // Disconnect
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage().'<br />';
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
?>
\\ ist kein Kommentar, '//' ist. –
danke behoben zur Klärung – newpie
Ich plante, eine Reihe von Variablen durch Kommata getrennt zu senden. – newpie