Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts

    MySQL Like Query

    Hi,

    I currently have the following code


    $sql = "select * from messages where id = '".$_POST['id']."' or from = '". $_POST['from'] ."' or subject = '". $_POST['subject'] ."' or message = '". $_POST['message'] ."' ORDER BY id DESC";


    What I would like to do is have a LIKE on subject and message, non-case sensitive but not sure how to do it?

    Can someone tell me how?

    Thanks
    Regards,
    CBG

  • #2
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Hi,

    Ok I have it sort of working

    with

    $sql = "select * from messages where id = '".$_POST['id']."' or from = '". $_POST['from'] ."' or subject LIKE '%". $_POST['subject'] ."%' or message LIKE '%". $_POST['message'] ."%' ORDER BY id DESC";


    But the problem is when I do a from search it comes back with all sorts, which I have tracked down to subject and message being blank on the search page.

    How can I make sure that subject and message has something when doing the query?
    Regards,
    CBG

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,597
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Heard of the if statement in PHP?

    Code:
    <?php
    ...
    $msg = "" . $_POST["message"];
    $subj = "" . $_POST["submect"];
    if ( strlen($msg) == 0 || strlen($subj) == 0 )
    {
        echo "No message or no subject given.  Aborting.";
        exit( "Invalid request" );
    }
    ... continue ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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