Jump to content

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";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.