Jump to content

[SOLVED] Need help with a query.


JSHINER

Recommended Posts

With the following:

$state = "CA";

$page['listings_state'] = RListing::getResults($db, $state, ($pageNum - 1) * $perPage, $perPage);

function getResults($db, $state, $offset, $limit) {
	$query = 'SELECT listing.id AS id, listing.views AS views, listing.price AS price, listing.type AS type, town.name AS town, town.state AS state, listing.town_id AS town_id FROM listing, town WHERE listing.town_id = town.id AND state = ' . $db->escape($state) . ' ORDER BY views DESC LIMIT 9';
	return $db->getArray($query);
}

 

I get the following error:

 

"Error in database query. Query was [sELECT listing.id AS id, listing.views AS views, listing.price AS price, listing.type AS type, town.name AS town, town.state AS state, listing.town_id AS town_id FROM listing, town WHERE listing.town_id = town.id AND state = CA ORDER BY views DESC LIMIT 9] and the error returned was [unknown column 'CA' in 'where clause']"

 

Any ideas why?

Link to comment
https://forums.phpfreaks.com/topic/87049-solved-need-help-with-a-query/
Share on other sites

Lets look at this part of the WHERE clause

AND state = ' . $db->escape($state) . '

 

First off you don't have the variable in quotes. Second you are using aliasing throughout the rest of the query, so why are you not using it for the column "state"?

 

So your query should be changed to

$query = "SELECT listing.id AS id, listing.views AS views, listing.price AS price, listing.type AS type, town.name AS town, town.state AS state, listing.town_id AS town_id FROM listing, town WHERE listing.town_id = town.id AND town.state = '".$db->escape($state)."' ORDER BY views DESC LIMIT 9";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.