作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数组 $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/
我是一名优秀的程序员,十分优秀!