Grow your CSS skills. Land your dream job.

PHP get where category does not equal these 3

  • # November 4, 2013 at 8:34 pm

    in a blog site im working on i have 3 categories of blogs and than a “other” category where the blog there does not match the 3 categories. So i want to select the categories that does not equel motivational, financial, personal. I tried something like this but it didn’t work.

    SELECT * FROM articles WHERE category !=’financial’, ‘motivational, ‘personal’

    # November 4, 2013 at 9:07 pm

    Try writing your SQL like this:

    SELECT *
    FROM articles
    WHERE category != ‘financial’,
    OR category != ‘motivational’,
    OR category != ‘personal’

    You have to tell SQL which operation to perform for each category

    Hope this helps!

    # November 4, 2013 at 9:37 pm

    Its giving me this error mysql_fetch_array() expects parameter 1 to be resource, boolean given in

    This is how i have it written

    $sql = mysql_query("SELECT * FROM articles WHERE category !='personal', OR category !='financial', OR category !='motivational'");
    
    # November 5, 2013 at 9:01 am

    Use “in” instead, much cleaner:

    SELECT * FROM articles WHERE category not in(‘financial’, ‘motivational’, ‘personal’);

    Should work!

    __
    # November 5, 2013 at 9:25 am

    SELECT *
    FROM articles
    WHERE category != ‘financial’
    OR category != ‘motivational’
    OR category != ‘personal’

    This won’t filter anything. Any given value will “not be” at least two of these, so you will be selecting everything. Use AND instead of OR. Alternatively, use IN as @dalbertsson suggested.

    mysql_fetch_array() expects parameter 1 to be resource, boolean given ...

    “boolean” is true or false. Your query returned false. (We know it’s false because mysql_query never returns true, but does return false – when there is an error.) You need to be checking your query result before trying to use it:

    <?php

    // no!
    $result = mysql_query( "some sql" );
    $row = mysql_fetch_array( $result );
    
    // yes!
    $result = mysql_query( "some sql" );
    if( $result ){
        $row = mysql_fetch_array( $result );
    }
    else{
        /* there was an error */
    }
    

    $sql = mysql_query("SELECT * FROM articles WHERE category !='personal', OR category !='financial', OR category !='motivational'");

    The error is coming from the commas you have before each OR – they don’t belong. (But don’t fix this query – use another (above)).

    And, finally (and I know this may not be possible right away), you should seriously consider not using the mysql_* functions. They are deprecated. (They haven’t been “recommended” since 2004.) mysqli or pdo are good alternatives.

    # November 5, 2013 at 9:50 am

    Thank you. That worked perfectly. Now i’m not sure how i can include character limits here. I’m not exactly sure how to set it combining all this. I want to limit the content characters to 500 in the other page where the article previews are displayed. Also descending order. How can i achieve that? this is what i currently have.

    $sql = mysql_query("SELECT * FROM articles WHERE category NOT IN('financial','motivational','personal')");
    
    __
    # November 5, 2013 at 10:00 am

    I want to limit the content characters to 500 in the other page where the article previews are displayed.

    example from another forum I frequent:

    <?php
    
    // say your article is in the var $article 
    // $preview starts as the entire article 
    $preview = $article
    
    // check if the preview needs to be shortened 
    if (strlen($preview) > 500) {
    
        // chop off at 500 characters
        $preview = substr( $preview,0,500 );
    
        // take off the last [possibly partial] word, for neatness
        $preview = preg_replace('~\s+\S+$~', '', $preview);
    
        // add ellipses [if desired]
        $preview .= "…";
    }
    // your 500-character-or-less preview 
    echo $preview;
    

    Also descending order.

    Descending order when sorted how?

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".