gpt4 book ai didi

php - 使用数组值选择查询

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

我在第一个查询中将所有值保存到数组 $ownco 中。通过第二个查询,我尝试获取表帖子的所有行,其中 id 的值与数组 $ownco 中的值相同。

两个错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'where clause'

: Array to string conversion in line 34

$hostname='localhost';
$user='root';
$password='';
$useron = $_COOKIE['username'];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=searchfood",$user,$password);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "SELECT id_post
FROM comments
WHERE username = '$useron'
ORDER BY id DESC"; // oder (longitude between $loo and $lo or latitude between $laa and $la) versuchen
if ($own = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$ownco = $own->fetchAll();
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}


try {
$dbh = new PDO("mysql:host=$hostname;dbname=searchfood",$user,$password);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "SELECT id, autorid, autor, date, longitude, latitude, title, text, town, time
FROM posts
WHERE id in (" . implode(",",$ownco) . ") // line 34
ORDER BY id DESC"; // oder (longitude between $loo and $lo or latitude between $laa and $la) versuchen
if ($resco = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$resultcom = $resco->fetchAll();
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}

最佳答案

您必须更改获取样式。目前您正在使用默认的“FETCH_BOTH”,其结果如下:

Array
(
[0] => Array
(
[name] => pear
[0] => pear

如果您将 fetchAll 更改为仅获取列,它应该按预期工作。

$ownco = $own->fetchAll(PDO::FETCH_COLUMN, 0);

结果:

Array
(
[0] = 1,
[1] = 3,

等等

关于php - 使用数组值选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32875431/

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