My error gives 1064 it worked before i inserted the where statement. Do i put the where before or after the from?
<?PHP
session_start();
include ("connection.php");
$city = $_POST['city'];
$state = $_POST['state'];
$results = array();
if( $query = $db->query("SELECT business_id, BusinessName, date, post ".
"FROM WolfeboroC.posts ". "WHERE city= '$city' && state='$state'".
"JOIN WolfeboroC.users ON users.recid = posts.business_id ".
"ORDER BY date DESC LIMIT 0, 500") )
{
while($record = $query->fetch_assoc())
{
$results[] = $record;
}
$query->close();
}
echo json_encode($results);
?>
"SELECT business_id, BusinessName, date, post ".
"FROM WolfeboroC.posts ". "WHERE city= '$city' && state='$state'".
"JOIN WolfeboroC.users ON users.recid = posts.business_id ".
"ORDER BY date DESC LIMIT 0, 500"
SHOULD BE
"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"
The JOIN verb should come before WHERE.
Your JOIN WolfeboroC.users
syntax is not having space before it, so it is going wrong to MySQL
.
Your code:
if( $query = $db->query("SELECT business_id, BusinessName, date, post ".
"FROM WolfeboroC.posts ". "WHERE city= '$city' && state='$state'".
"JOIN WolfeboroC.users ON users.recid = posts.business_id ".
"ORDER BY date DESC LIMIT 0, 500") )
Corrected Code:
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' && state='$state' ". "ORDER BY date DESC LIMIT 0, 500") )
Also, you are selecting fields: business_id, BusinessName, date, post
.
These fields are ambigious.
You are joining two tables.
Hence you should specify, which field comes from which Table.
e.g. users.business_id, users.BusinessName, posts.date, posts.post
Here, MySQL
has to guess which field is of which table.
If a field exists for both the tables, it should throw an error.
Also, in the SQL, WolfeboroC.users
You should not specify the database name.
It should be `users' only.
As database name is configurable for most of the sites, it will change.