datoshway Posted January 16, 2011 Share Posted January 16, 2011 Can someone help me add a else statement to throw a error if no results are found? Here is my query. <?php require "_controller.php"; db_connect(); db_select(); $strQuery = "SELECT * FROM tblXXX GROUP BY intXXX"; $queryGetNews = db_query($strQuery); ?> <?php if (db_num_rows($queryGetNews) > 0) { while ($objRow = db_fetch_object($queryGetArticle)) { $intXXX = intval($objRow->intID); $txtXXX = strip_tags(stripslashes($objRow->txtXXXe)); ?> <?php echo $txtXXX; ?> <?php } db_free_result($queryGetNews); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/224648-throw-error-message/ Share on other sites More sharing options...
isedeasy Posted January 16, 2011 Share Posted January 16, 2011 if (db_num_rows($queryGetNews) > 0) { //while loop } else { //throw error message } Quote Link to comment https://forums.phpfreaks.com/topic/224648-throw-error-message/#findComment-1160403 Share on other sites More sharing options...
datoshway Posted January 16, 2011 Author Share Posted January 16, 2011 Hey thanks for the reply, I tried to put that into the query but still getting a blank page. Where exactly should that go? Just want to make sure i'm placing it correctly? Quote Link to comment https://forums.phpfreaks.com/topic/224648-throw-error-message/#findComment-1160413 Share on other sites More sharing options...
joel24 Posted January 16, 2011 Share Posted January 16, 2011 did you put anything in the else part of the if statement? if (db_num_rows($queryGetNews) > 0) { //while loop } else { //throw error message echo "error has occured or no rows returned<br/>".mysql_error(); } Quote Link to comment https://forums.phpfreaks.com/topic/224648-throw-error-message/#findComment-1160426 Share on other sites More sharing options...
datoshway Posted January 16, 2011 Author Share Posted January 16, 2011 No go, I'm getting a syntax error. This is what it looks like after what you sent me. <?php if (db_num_rows($queryGetNews) > 0) { while ($objRow= db_fetch_object($queryGetArticle)) { $intRaceID = intval($objRow->intID); $txtUpdate = strip_tags(stripslashes($objRow->txtUpdate)); $dtEntered = stripslashes($objRow->dtEntered); ?> <?php echo $txtUpdate; ?> <?php echo date("g:i A", strtotime($dtEntered)); ?> <?php } else { //throw error message echo "error has occured or no rows returned<br/>".mysql_error(); }?> Quote Link to comment https://forums.phpfreaks.com/topic/224648-throw-error-message/#findComment-1160447 Share on other sites More sharing options...
joel24 Posted January 17, 2011 Share Posted January 17, 2011 you don't need to open and close the <?php ?> tags so often, you only need to open at the start and close after the end of the PHP code. The syntax error you're getting would be because you are missing a set of closing brackets for the while loop <?php if (db_num_rows($queryGetNews) > 0) { while ($objRow= db_fetch_object($queryGetArticle)) { //open while loop $intRaceID = intval($objRow->intID); $txtUpdate = strip_tags(stripslashes($objRow->txtUpdate)); $dtEntered = stripslashes($objRow->dtEntered); echo $txtUpdate; echo date("g:i A", strtotime($dtEntered)); } //close while loop } else { //close IF and add else statement //throw error message echo "error has occured or no rows returned<br/>".mysql_error(); }?> Quote Link to comment https://forums.phpfreaks.com/topic/224648-throw-error-message/#findComment-1160476 Share on other sites More sharing options...
datoshway Posted January 17, 2011 Author Share Posted January 17, 2011 Ohh ok I see why know. Thanks Man! Works now! Quote Link to comment https://forums.phpfreaks.com/topic/224648-throw-error-message/#findComment-1160482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.