gpt4 book ai didi

PHP PDO post 字符串列表 INWhere 子句

转载 作者:行者123 更新时间:2023-11-29 11:25:28 25 4
gpt4 key购买 nike

我几乎可以使用以下代码,但不熟悉如何像我的测试一样以数组形式将我的列表从最终用户发布到 $ids 。最终用户会向我发送一个列表“3,5,7,8...)

转换为 POST 语句时如何模拟“$ids = array(1,2,3)”?

<?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);
}
?>

感谢您的帮助。

================================================== ===============

如果它对任何人有帮助,这是完整的脚本:

<?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);
}
?>

最佳答案

您的 $_POST["idsvar"] 是一个字符串,而不是一个数组。使用 explode 将其在每个逗号处拆分为一个数组。

$test = explode(',', '1,2,3');

演示:https://eval.in/603133

就您而言:

$ids = explode(',', $_POST["idsvar"]);

count($ids) 在静态示例中有效,因为您将 $ids 定义为数组 ($ids = array(1,2,3 ))。

或者如果您还想验证输入,您可以使用正则表达式。

(\d+)(?:,|\z)

https://regex101.com/r/dI5fN4/1

关于PHP PDO post 字符串列表 INWhere 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38297819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com