-4

当我在 mysql 工作台中运行此代码时,它可以工作,但在我的 php 中它只返回一个空数组。

<?PHP
session_start();

$results = array();
if( $query =  $db->query("SELECT business_id, BusinessName, date, post ".
  "FROM WolfeboroC.posts ".
  "JOIN WolfeboroC.users ON users.recid = posts.business_id ".
  "WHERE City= '$city' AND State='$state'".
  "ORDER BY date DESC LIMIT 0, 500") ) 
{
  while($record = $query->fetch_assoc())
  {
    $results[] = $record;
  }
  $query->close();
}

echo json_encode($results);
?>
4

1 回答 1

0

首先,你不能使用结构

FROM
WHERE
JOIN

你必须使用

FROM
JOIN
WHERE

其次,您在末尾缺少一个空格'$state'"

"FROM WolfeboroC.posts ". "WHERE city= '$city' && state='$state'".
"JOIN WolfeboroC.users ON users.recid = posts.business_id ".

因此,正如您所提到的,我很确定这在 MySQL 上并不适用。

还有一些PHP错误。使用此代码:

if($result = $db->query(
  "SELECT business_id, BusinessName, date, post ".
  "FROM WolfeboroC.posts ".
  "JOIN WolfeboroC.users ON users.recid = posts.business_id ".
  "WHERE City= '$city' AND State='$state' ".
  "ORDER BY date DESC LIMIT 0, 500")) {
  while($record = $result->fetch_assoc()) {
    $results[] = $record;
  }
  $result->close();
}
于 2013-10-26T04:55:48.007 回答