Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End PHP get where category does not equal these 3

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #155184
    Anonymous
    Inactive

    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’

    #155185
    Justin
    Participant

    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!

    #155186
    Anonymous
    Inactive

    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'");
    
    #155229
    [email protected]
    Participant

    Use “in” instead, much cleaner:

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

    Should work!

    #155238
    __
    Participant

    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.

    #155243
    Anonymous
    Inactive

    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')");
    
    #155244
    __
    Participant

    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)
  • The forum ‘Back End’ is closed to new topics and replies.