使用sql查询从2个表中使用join检索数据不起作用,因为我希望这是使用过的查询:
$result2 = $wpdb->get_results('select site_info.siteID, owner_info.ownerNAME,
site_info.siteNAME, site_info.equipmentTYPE
from `site_info`
INNER JOIN `owner_info` on site_info.ownerID = owner_info.ownerID
where owner_info.ownerID = 159');
foreach($result2 as $result) {
echo join(', ', $result), '<br/>';
echo "<br/>";
}
我从2个表中检索数据但结果是:
Warning: join(): Invalid arguments passed in /opt/lampp/htdocs/wordpress/wp-content/themes/wp-portfolio/search-info.php on line 43
如果有人能告诉我我做错了什么。
答案 0 :(得分:0)
join()是implode()
的php函数别名http://php.net/manual/en/function.implode.php
传递给join()的第二个参数不是数组,但是join方法需要一个数组作为第二个参数,这会导致问题
$ result是具有以下形式的对象: stdClass对象([siteID] =&gt; BAH004 [siteNAME] =&gt; XXXXXXX [设备类型] =&gt; XXXXXXX)
也许尝试这样的事情:
foreach($result2 as $result) {
echo join(', ', array($result->siteID, $result->ownerNAME, $result->siteNAME, $result->equipmentTYPE)), '<br/>';
echo "<br/>";
}
你也可以试试这个更简单:
foreach($result2 as $result) {
$result = (array)$result; // convert the object to an array
echo join(', ', $result), '<br/>';
echo "<br/>";
}