Solved

Trying to construct an sql query string

Posted on 2007-04-08
3
236 Views
Last Modified: 2012-05-05
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_general.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_theme_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,
0
Comment
Question by:cemlouis
  • 2
3 Comments
 
LVL 50

Accepted Solution

by:
Steve Bink earned 250 total points
ID: 18872459
String concatenation is done with the period.

            $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
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 18872461
Also:

 $sql_tail_query .= "theme_id='$theme_id' AND ";
0
 

Author Comment

by:cemlouis
ID: 18872474
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,
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now