gpt4 book ai didi

php - MySQL Where 子句中的数组

转载 作者:行者123 更新时间:2023-11-29 08:42:26 24 4
gpt4 key购买 nike

我有一个数组 $ziparray,我试图在 MySQL 查询中使用它来检查名为“zip”的数据库列。以下代码对我来说似乎合乎逻辑,但出现错误:

Catchable fatal error: Object of class stdClass could not be converted to string in...

$ziparrayimplode = implode(",", $ziparray);
$listinghistoryquery = "SELECT * FROM listings WHERE zip IN ($ziparrayimplode) ORDER BY list_ts DESC";
$listinghistory = mysql_query($listinghistoryquery) or die('SQL Error :: '.mysql_error());

我绝对确定 $ziparray 是一个数组。谁能告诉我如何使这个查询工作?这是用于创建数组的代码..

$countyzipquery = mysql_query("SELECT city_zip FROM dev_cities WHERE city_state='$state' AND city_county='$county' AND city_name='$city'");
$ziparray = array();
while (($ziparrayrow = mysql_fetch_object($countyzipquery)) !== FALSE) {
$ziparray[] = $ziparrayrow;

这是 var_dump($ziparray) 的输出

array(1) { [0]=> object(stdClass)#3 (1) { ["city_zip"]=> string(5) "63028" } }

最佳答案

while (($ziparrayrow = mysql_fetch_object($countyzipquery)) !== FALSE) {

您从数据库中获取对象,然后将这些完整对象存储到您的$ziparray 数组中。您无法连接 stdClass 对象,因为它们无法转换为字符串(对象需要为此定义一个 __toString 方法,而 stdClass 实例则可以没有这样的方法)。

您很可能想要获取数组(mysql_fetch_array 而不是 mysql_fetch_object),然后仅存储第一个元素 ($ziparrayrow[0] ) 在您的 $ziparray 中。或者,您可以保留 mysql_fetch_object,但更改 $ziparray 中元素的累积:

$ziparray[] = $ziparrayrow->city_zip;

关于php - MySQL Where 子句中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13435716/

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