Forum Moderators: open

Message Too Old, No Replies

Help with query

         

jacko4

1:54 pm on Jul 23, 2006 (gmt 0)

10+ Year Member



I need help with this where statement. I have a variable defined above as $idtype='1'. The problem occurs in the mysql query.
Which is the following.

$sql = "SELECT p.*, a.id AS agentid, a.name AS agent, a.user AS user, c.name AS company, c.id AS companyid, t.name AS type, p.type AS typeid "
. "\n FROM (#__hp_properties AS p, #__hp_companies AS c)"
. "\n LEFT JOIN #__hp_agents AS a ON p.agent = a.id"
. "\n LEFT JOIN #__hp_prop_types AS t ON p.type = t.id"
. "\n LEFT JOIN #__hp_featured AS f ON f.property = p.id"
. "\n WHERE p.published='1' AND p.approved='1' AND t.published='1'"
.(count( $where )? " AND " . implode( ' AND ', $where ) : "")
.(($which == "featured")? "\nAND p.featured='1' AND p.type='". $idtype."" : "")

Now if on the last line I have:
.(($which == "featured")? "\nAND p.featured='1' AND p.type='1'" : "")

I get the results I am looking for with p.type='1'
I want to be able to bring in the changing variable from above though, the $idtype. What can I do to fix it so that it will show p.type=$idtype with correct syntax?
Is my syntax wrong?

zCat

4:06 pm on Jul 23, 2006 (gmt 0)

10+ Year Member



.(($which == "featured")? "\nAND p.featured='1' AND p.type='". $idtype."" : "")

Possibly that should be a sinqle quote following $idtype?

FalseDawn

12:08 am on Jul 24, 2006 (gmt 0)

10+ Year Member



If the columns are numeric, there's no need to enclose the values in single quotes anyway.
eg:
.(($which == "featured")? "\nAND p.featured=1 AND p.type=".$idtype : "")

I'd also remove all those \n cluttering up the SQL.

coopster

8:12 pm on Jul 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, jacko4.

Did you get this figured out? The query as you have it written is missing the last apostrophe as the others have suggested. You can troubleshoot errors such as these much easier if you dump your query to the browser, too. Here is what the last line looks like when we dump it out:

$sql = (statment here); 
print '<pre>'; print($sql); exit('</pre>');
// AND p.featured='1' AND p.type='1