- This topic is empty.
-
AuthorPosts
-
November 4, 2013 at 8:34 pm #155184AnonymousInactive
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 #155185JustinParticipantTry 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 #155186AnonymousInactiveIts 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 #155229[email protected]ParticipantUse “in” instead, much cleaner:
SELECT * FROM articles WHERE category not in(‘financial’, ‘motivational’, ‘personal’);
Should work!
November 5, 2013 at 9:25 am #155238__ParticipantSELECT *
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 ofOR
. Alternatively, useIN
as @dalbertsson suggested.mysql_fetch_array() expects parameter 1 to be resource, boolean given ...
“boolean” is
true
orfalse
. Your query returnedfalse
. (We know it’s false because mysql_query never returnstrue
, 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
orpdo
are good alternatives.November 5, 2013 at 9:50 am #155243AnonymousInactiveThank 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 #155244__ParticipantI 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?
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.