[英]select query with array values
I save with the first query any values into an array $ownco
. 我将第一个查询的所有值保存到数组
$ownco
。 With the second query I try to get all rows of the table posts where the id
have the same values as in the array $ownco
. 对于第二个查询,我尝试获取表帖子的所有行,其中
id
具有与数组$ownco
相同的值。
two errors: 两个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'where clause'
SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ Array”
: Array to string conversion in line 34
:第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();
}
You have to change the fetch style. 您必须更改提取样式。 Currently you are using the default "FETCH_BOTH" which results in something like:
当前,您正在使用默认的“ FETCH_BOTH”,结果如下:
Array
(
[0] => Array
(
[name] => pear
[0] => pear
If you change the fetchAll to only fetch the column it should work as expected. 如果将fetchAll更改为仅获取列,则它将按预期工作。
$ownco = $own->fetchAll(PDO::FETCH_COLUMN, 0);
Results: 结果:
Array
(
[0] = 1,
[1] = 3,
etc. 等等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.