SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SELECT * FROM $table where table field contains this keyphrase :: help please

    Hi guys

    Well, we have all done things like
    PHP Code:
    "SELECT * FROM $mytable WHERE tableinfo = '$tableinfo'" 
    I wanted a SELECT that will not try to match the tableinfo data (what's there in the db table) to the variable $tableinfo, but where the tableinfo field (db table) is a field of type text (say from a textarea) and $tableinfo is a keyword, and the aim is to find if the keyword $textinfo in present in the text field textarea.

    Any idea? Thanks.

  2. #2
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solved ... LIKE ...

  3. #3
    SitePoint Addict evilone's Avatar
    Join Date
    Oct 2004
    Location
    Estonia
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you mean like search words from text?

  4. #4
    SitePoint Addict evilone's Avatar
    Join Date
    Oct 2004
    Location
    Estonia
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep, you're right

    Code:
    SELECT * FROM $mytable WHERE tableinfo LIKE '$tableinfo%'

  5. #5
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah ... thanks.

  6. #6
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well it was working like
    PHP Code:
    "SELECT * FROM $mytable WHERE tableinfo = '%$tableinfo%'" 
    ... but here's the problem. Say, $tableinfo is let's, in that case, the query is becoming
    PHP Code:
    "SELECT * FROM $mytable WHERE tableinfo = '%let's%'" 
    any idea how to deal with this
    PHP Code:

    ?

    Besides, I was also wondering how to look for the presence of multiple words, seperated by a comma, for example. I made the following, but it doesn't work, I mean, search returns zero result, even when it shouldn't, when i put more than one keywords ...
    PHP Code:
    $pos strrpos($keyword",");
        if (
    $pos === true) { 
            
    $keyword explode(","$keyword);
                foreach (
    $keyword as $i) {
                
    $where .= " AND profile LIKE '%$i%'";
            }
        } else {
        
    $where .= " AND profile LIKE '%$keyword%'";
        } 
    any help? Thanks.

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You should never, ever be putting uncleaned user input into a SQL query. Fear the SQL Injection attack.

    PHP Code:
    $sql "SELECT * FROM $mytable WHERE tableinfo LIKE '%" mysql_real_escape_string($tableinfo) . "%'"

  8. #8
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Dan, well, I tried it using the mysql_real_escape_string(), and found that search was returning no result ... so, i took it out to check how it looks without this function, and surprise, surprise, ... now even without this function I am getting no result ... i went for echo $query, and though the $query seems to be working perfectly, I'm getting no result ...

  9. #9
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Echo the query and run it outside PHP -- at the MySQL command line or through phpMyAdmin. If you get results there, something's wrong with your code, but if you don't, something's wrong with the query or the data.

  10. #10
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bizarre bizarre ... it's working again ...

  11. #11
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    saw u hae just replied ... thanks Dan ... it's working again, i did some small changes here and there ...

  12. #12
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way, can you tell me why this is not working >
    PHP Code:
    if ($keyword != '') {
        
    $pos strrpos($keyword",");
        if (
    $pos === true) { 
            
    $keyword explode(","$keyword);
                foreach (
    $keyword as $i) {
                
    $where .= " AND profile LIKE '%$i%'";
            }
        } else {
        
    $where .= " AND profile LIKE '%$keyword%'";
        }

    if I'm not asking you too much ...

    The idea is to let users enter more than one search terms, seperated by commas, and to find out relevant result ... it's working for one keyword, but not for multiple keywords ... means the part
    PHP Code:
    $keyword explode(","$keyword); 
                foreach (
    $keyword as $i) { 
                
    $where .= " AND profile LIKE '%$i%'"
            } 
    is not working

  13. #13
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Best bet is to again echo the query and take a look at it. Is it what you expected, and is requiring a match on every word entered what you wanted?

  14. #14
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think this problem is solved, but i'm doing an error elsewhere ... but yeah, you remember I added this yesterday
    PHP Code:
    $page min($page$numPages) ? min($page$numPages) : 1
    ? Before I was getting -10, 10, and after using this, i've just found that i am getting 0, 1010 ... but this is not hampering anything visually .. so not a major problem it seems

  15. #15
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That should give you 1010 results back Unexpected but won't cause an error. I don't think the problem is on that line, and I don't really have time to duplicate all your code to test it :/

  16. #16
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, this is not causing any error, you are right ... i'll try to debug it later, if i can ... or it's working fine as well with this code ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •