Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Junior Member
    Join Date
    Feb 2005
    Posts
    3
    Member #
    8996
    My first PHP/MySql help request so I hope I'm giving enough information. Here's the PHP/Mysql code in question:

    $video_data = "SELECT * FROM video WHERE ((video_id = $listing_implode) AND video_name LIKE '".$_GET['sortby']."%') ORDER BY video_name limit $rowstart,$page_count";

    I want this to select the row from VIDEO if video_id matches $listing_implode and the first letter of video_name matches sortby.

    If i strip out --> AND video_name LIKE '".$_GET['sortby']."%' <-- it works (it returns all rows where video_id matches $listing_implode).

    If I strip out --> (video_id = $listing_implode) <-- it works and returns all rows that match LIKE.

    My problem...when I combine these two bits it does not work as it should.

    No error codes, it simply lists all rows that match video_id but the LIKE feature does nothing.

    I've stared at it for hours now...and it is time someone slaps me with the obvious.


  2.  

  3. #2
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    I tried running a similar query on a different table to see if I could generate the same problem, but it worked fine.

    You can skip all the parenthesis BTW. remember to put single quotes around $listing_implode if it's not an integer. If it is an integer, the query will still work with single quotes around it.

    As long as all variables holds valid data, I don't see why it shouldn't work. I assume the $_GET['sortby'] holds a single letter.
    S. Rosland

  4. #3
    Junior Member
    Join Date
    Feb 2005
    Posts
    3
    Member #
    8996
    Hmmm...here's more information

    As a newbie I assumed (and still do) syntax is my problem but that didn't work (I'm glad to have it looking cleaner, though ).

    Here's what I have now:

    $video_data = "SELECT * FROM video WHERE video_id = '$listing_implode' AND video_name LIKE '".$_GET['sortby']."%' ORDER BY video_name limit $rowstart,$page_count";

    Even though it seems to work both ways, here's some information on the $listing_implode variable...perhaps this is the problem.

    --> $listing_implode = implode(" OR video_id = ",$test_variable);

    It is a imploded array which echos like this:

    listing_implode: 13 OR video_id = 9 OR video_id = 5 OR video_id = 12 OR video_id = 11 OR video_id = 14

    So, I take the array, implode it to a string, and use the string to complete the mysql Select argument.

    Again, this works fine except when combined with LIKE.

    (after writing this, I think the first question will be "what the &%^$@ are you doing?)

    Thanks.

  5. #4
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    If the imploded variable $listing_implode contains a series of different conditions, then you will have to use parenthesis. Otherwise, the LIKE clause will only be applied to the last OR clause.

    Ex:
    Code:
    SELECT * FROM table_name WHERE id=12 OR id=10 OR id=7 AND column_name LIKE 'wa%'
    The LIKE clause will only be applied to the row where id=7. You would have to have parenthesis encapsulating the WHERE conditions to enable the LIKE clause to work on all occurences:

    Code:
    SELECT * FROM table_name WHERE (id=12 OR id=10 OR id=7) AND column_name LIKE 'wa%'
    See if that gives the desired result.
    S. Rosland

  6. #5
    Junior Member
    Join Date
    Feb 2005
    Posts
    3
    Member #
    8996
    The long night is over...

    And...thank you (banging head into keyboard).

    It was doing exactly as you said...I couldn't see the problem until you pointed out that it was executing the last variable and that is exactly what it was doing.

    Interesting problem...I'm sure this experience will come in handy down the road. Thanks again.

    :classic:


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:16 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com