cemlouis
asked on
Trying to construct an sql query string
Hello,
I am making a section for my handmade themes on my site(anyway this is not the point). I am trying to construct a sql_query string but the output is mostly 0 I am not new to php but probably I am missing something out in the crowd... The below code explains almost everything:
<?php
include "database_connection_gener al.inc";
$property_id = "2";
// I need to get theme ids which has a property_id = 2
$sql_theme_id_get = mysql_query("SELECT theme_id FROM theme_property_relation WHERE property_id='$property_id' ");
while ($row = mysql_fetch_array($sql_the me_id_get) ) {
$theme_id = $row["theme_id"];
$sql_tail_query += "theme_id='$theme_id' AND ";
}
$sql_head_query = "SELECT * FROM theme WHERE "; // adding the head part for the query
$sql_extended_query = $sql_head_query + $sql_tail_query; // adding the tail part but there is a "AND " at the end that must be cropped!!!
$sql_total_query = substr($sql_extended_query , 0, -4); // cropping the last 4 characters "AND "
$sql_final_query = $sql_total_query + " ORDER BY theme_added DESC LIMIT 0 , 10"; // adding the order part
echo "$sql_final_query";
/* Output should be like:
SELECT * FROM theme WHERE theme_id='2' AND theme_id='15' AND theme_id='74' AND theme_id='103' ORDER BY theme_added DESC LIMIT 0 , 10
but getting 0
*/
?>
Thank you,
I am making a section for my handmade themes on my site(anyway this is not the point). I am trying to construct a sql_query string but the output is mostly 0 I am not new to php but probably I am missing something out in the crowd... The below code explains almost everything:
<?php
include "database_connection_gener
$property_id = "2";
// I need to get theme ids which has a property_id = 2
$sql_theme_id_get = mysql_query("SELECT theme_id FROM theme_property_relation WHERE property_id='$property_id'
while ($row = mysql_fetch_array($sql_the
$theme_id = $row["theme_id"];
$sql_tail_query += "theme_id='$theme_id' AND ";
}
$sql_head_query = "SELECT * FROM theme WHERE "; // adding the head part for the query
$sql_extended_query = $sql_head_query + $sql_tail_query; // adding the tail part but there is a "AND " at the end that must be cropped!!!
$sql_total_query = substr($sql_extended_query
$sql_final_query = $sql_total_query + " ORDER BY theme_added DESC LIMIT 0 , 10"; // adding the order part
echo "$sql_final_query";
/* Output should be like:
SELECT * FROM theme WHERE theme_id='2' AND theme_id='15' AND theme_id='74' AND theme_id='103' ORDER BY theme_added DESC LIMIT 0 , 10
but getting 0
*/
?>
Thank you,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello routinet,
thank you, I get ashamed of making this mistake :( I am messing with C# these days probably that's the source of the mistake...
Regards,
thank you, I get ashamed of making this mistake :( I am messing with C# these days probably that's the source of the mistake...
Regards,
$sql_tail_query .= "theme_id='$theme_id' AND ";