Jump to content

Searching more than 1 column


OLM3CA

Recommended Posts

I want to search a word more than one column in table but result doesnt change while I add the other columns.Is there something wrong with the usage of the below code ?

[code]<?php
                $query="SELECT * FROM link WHERE words LIKE '%".$sword."%' OR url LIKE '%".$sword."%'
AND status ='1' LIMIT $from, $max_results";
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/21331-searching-more-than-1-column/
Share on other sites

When mixing OR and AND use (..) to specify the logic

Is it
[code]
$query="SELECT * FROM link WHERE
    (words LIKE '%".$sword."%')
    OR
    ((url LIKE '%".$sword."%') AND (status ='1'))
    LIMIT $from, $max_results";   
[/code]

or is it
[code]     
$query="SELECT * FROM link WHERE
        ((words LIKE '%".$sword."%') OR (url LIKE '%".$sword."%'))
AND (status ='1')
        LIMIT $from, $max_results";   
[/code]
Thank you I have done that query but I got error,Something wrong with the query but what ?

[code] Warning: mysql_result(): supplied argument is not a valid MySQL result resource in ... [/code]

[code]<?php
             
                // This query works fine I think.

$query="SELECT * FROM links
WHERE ((keywords LIKE '%".$sword."%')
        OR (url LIKE '%".$sword."%') OR (aciklama LIKE '%".$sword."%')
OR (baslik LIKE '%".$sword."%'))
      AND (status ='1') LIMIT $from, $max_results";

                $result=mysql_query($query);


                // The problem is in this query I guess

                $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM links
      WHERE ((keywords LIKE '%".$sword."%')
      OR (url LIKE '%".$sword."%') OR (aciklama LIKE '%".$sword."%')
      OR (baslik LIKE '%".$sword."%'))
    AND (status ='1')"),0);
?>[/code]
When using mysql_result(), don't use inline code, assign the result to a variable first...

change this:
[code=php:0]$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM links WHERE ((keywords LIKE '%".$sword."%')
OR (url LIKE '%".$sword."%') OR (aciklama LIKE '%".$sword."%') OR (baslik LIKE '%".$sword."%')) AND (status ='1')"),0);[/code]


to this:
[code=php:0]$totalsql = "SELECT COUNT(*) as Num FROM links WHERE ((keywords LIKE '%".$sword."%') OR (url LIKE '%".$sword."%')
OR (aciklama LIKE '%".$sword."%') OR (baslik LIKE '%".$sword."%')) AND (status ='1')";
$sqlresult = mysql_query($totalsql);
$total_results = mysql_result($sqlresult, 0);
[/code]


Regards
Huggie
Try to find out why query gives error (I agree Huggie, don't use mysql_query inline).
Try
[code]<?php
$result = mysql_query("SELECT COUNT(*) as Num FROM links
      WHERE ((keywords LIKE '%".$sword."%')
      OR (url LIKE '%".$sword."%') OR (aciklama LIKE '%".$sword."%')
      OR (baslik LIKE '%".$sword."%'))
    AND (status ='1')") or die (mysql_error());

$total_results = mysql_result($result, 0);
?>[/code]
[quote]Thank you very much I am so ashamed that I have done a vaiable name mistake [/quote]

Haven't we all.

Get into the habit of using ... or die(mysql_error()), or better still, something like this, so you can also print the query giving the problem
[code]
$sql = "SELECT bla etc"
$result = mysql_query ($sql) or die (mysql_error() . " in <p>$sql</p>");
[/code]

Archived

This topic is now archived and is closed to further replies.

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