Jump to content

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]
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.