SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    what is wrong with my simple query??? help

    Hi,

    Basically I am trying to qet a working query for Sample3 and Sample4 --- they do not have the wildcard % character and use AND instead of OR.
    Hope someone can help me out or point me in the right direction.


    Sample1
    // this works, except I dont want the wildcard % in there, I want it to match perfectly
    Code:
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    $query = "SELECT * FROM art_chk WHERE chk like '%$search_chknumber%' and code like '%$search_prdcode%' and detailed_description like '%$search_prdname%' and general_description like '%$search_description%' and art like '%$search_art%' ";
    Sample2
    // this works, except its OR and I need it to be AND as multiple things have to match
    Code:
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    $query = "SELECT * FROM art_chk WHERE chk like '$search_chknumber' OR code like '$search_prdcode' OR detailed_description like '$search_prdname' OR general_description like '$search_description' OR art like '$search_art' ";
    Sample3
    // this does not work, returns no results
    Code:
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    $query = "SELECT * FROM art_chk WHERE chk like '$search_chknumber' and code like '$search_prdcode' and detailed_description like '$search_prdname' and general_description like '$search_description' and art like '$search_art' ";
    Sample4
    //does not work, returns no results
    Code:
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    $query = "SELECT * FROM art_chk WHERE chk='$search_chknumber' and code='$search_prdcode' and detailed_description='$search_prdname' and general_description='$search_description' and art='$search_art' ";

    Thanks

  2. #2
    SitePoint Member
    Join Date
    Dec 2010
    Location
    Silicon Valley
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want it to match perfectly then use the equal operator.

    Code SQL:
    SELECT * FROM art_chk
    WHERE chk = '$search_chknumber' 
    AND code = '$search_prdcode'
    AND detailed_description = '$search_prdname'
    AND general_description = '$search_description'
    AND art = '$search_art'

    Please read up on SQL Injections: http://php.net/manual/en/security.da...-injection.php

  3. #3
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using the = operator in sample 4 of my initial post - I even tried using your code and it returns no results, and no errors.

    And, the rest of my code I am assuming is ok because when I change the query around (as in change it to sample1 or sample2 from my initial post) it works and I get the results ...

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check for possible spaces in your db data and/or your variables
    Also echo your query to make sure it looks like you want.
    Test your query in Cpanel PHPAdmin to see what results you get

  5. #5
    SitePoint Member
    Join Date
    Dec 2010
    Location
    Silicon Valley
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As litebearer suggested, echo $query and see if 1. that's what you expected to see and 2. run that query in the command line/phpadmin or whatever you're using to access mysql.


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
  •