Jump to content

Return data from more than one column


mackin

Recommended Posts

 

 

 $searchterm_standard_listing = "-1";
    if (isset($_POST['searchterm'])) {
      $searchterm_standard_listing = $_POST['searchterm'];
    }
    mysql_select_db($database_contractors, $contractors);

    $query_standard_listing = sprintf("SELECT * FROM hotels WHERE est_town LIKE  %s AND featured = 'N' AND premier = 'N'", GetSQLValueString($searchterm_standard_listing, "text"));

    $standard_listing = mysql_query($query_standard_listing, $contractors) or die(mysql_error());
    $row_standard_listing = mysql_fetch_assoc($standard_listing);

    $totalRows_standard_listing = mysql_num_rows($standard_listing);

[quote][/quote]

 

 

I have this code which dreamweaver "helped" me create - i want it to use the searchterm to return the contents of 2 more columns (est_county and est_postcode) I have tried numerous ways to make this work such as

 

 

 

 WHERE est_town OR est_county OR est_postcode LIKE ........

 

but that returns Warning: sprintf() [function.sprintf]: Too few arguments.

 

Any suggestions to make this work?

Link to comment
Share on other sites

sprintf("SELECT * FROM hotels WHERE est_town LIKE  %s AND featured = 'N' AND premier = 'N'", 
  GetSQLValueString($searchterm_standard_listing, "text"))

"%s" is a placeholder that is replaced by the value (in this case returned by "GetSQLValueStrin()"

 

Your proposed SQL code is not valid

WHERE est_town OR est_county OR est_postcode LIKE ........

 

You would have to use

 

WHERE est_town LIKE ... OR est_county LIKE ... OR est_postcode LIKE ...

 

To do that with sprintf() you can use numbered placeholders:

 

sprintf("SELECT * FROM hotels 
WHERE (est_town LIKE '%1\$s' OR est_county LIKE '%1\$s' OR est_postcode LIKE '%1\$s') 
AND featured = 'N' AND premier = 'N'", 
  GetSQLValueString($searchterm_standard_listing, "text"))

 

which will put the first value in each "%1$s" placeholder. Take note of the parenthesis to separate the OR's from the AND's; and the backslashes to keep PHP from trying to interpret the "$s" as a variable. Also, note that you have to use wildcards to make the LIKE effective. The "%" symbol will match any number of characters.  So you would use "%me%' to match the term "me" with anything before it and/or anything after it. Also, the term needs to be in quotes. Without seeing the GetSQLValueString() definition, we can't tell if it is returning the "%" and/or the quotes. The final SQL would look something like:

 

SELECT * FROM hotels 
WHERE (est_town LIKE '%here%' OR est_county LIKE '%here%' OR est_postcode LIKE '%here%') 
AND featured = 'N' AND premier = 'N'

 

Using LIKE with a "%" at the beginning of the term will NOT allow the query to use an index. So it will have to scan every row in the table. Using LIKE without any "%" is useless and would be better written as "=" (instead of LIKE).

Link to comment
Share on other sites

SELECT * FROM hotels WHERE (est_town LIKE %1\$s OR est_county LIKE %1\$s OR est_postcode LIKE %1\$s) AND featured = 'N' AND premier = 'N'", GetSQLValueString($colname_standard_listing, "text")

 

Cheers David - I have gone with the above code - had to remove the apostrophes around the %1\$s for it to work but now seems to be working like a dream - will have to test it to death now - thanks very much for your help

Link to comment
Share on other sites

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.