Jump to content

Archived

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

OLM3CA

Searching more than 1 column

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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Thank you for your reply but the same problem still continious...

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Thank you very much I am so ashamed that I have done a vaiable name mistake I am sorry thank you for your help but I have learned not to use mysql_result in line code thank you.

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites

×

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.