jandrews Posted April 1, 2009 Share Posted April 1, 2009 Hello all, I am using php to query data from a mysql database and need to know how to code php to say 'if there are no results in this field, include this page' The results that are being queried is a text field the following code just echos the error message 'no tabs!' even though without the if statement, it shows the text in 'tab.php' just fine. <?php if ($results){ include 'tab.php' ; } else { echo 'no tabs!'; } ?> As you can probably guess, I am new to php. What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/ Share on other sites More sharing options...
JeanieTallis Posted April 1, 2009 Share Posted April 1, 2009 Curious question, what does tab.php do? Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798710 Share on other sites More sharing options...
jandrews Posted April 1, 2009 Author Share Posted April 1, 2009 Curious question, what does tab.php do? Just this for now but i will later use this file for other more complex things. <?php { echo ($row_Tab['Tab']); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798712 Share on other sites More sharing options...
JeanieTallis Posted April 1, 2009 Share Posted April 1, 2009 From what I see if ($results) { What results? eg: with a login, it would be if ($results -> logged_in) { page to display to logged in users } else { page to display to none logged in users } So the problem I'm guessing would be, the 'form' as I'm guessing it could be, doesnt know what the results have to be to display tab.php How do you get this 'tab' to come up? is it via the form with a button or... give me details on what you to. Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798715 Share on other sites More sharing options...
Zhadus Posted April 1, 2009 Share Posted April 1, 2009 Just as JeanieTallis stated, you weren't real clear as to what kind of value $results has. From your description I assume $results is a mysql_query(). If that's the case, you can do this: <?php if (mysql_num_rows($results)) { include_once('tab.php'); } else { echo 'no tabs!'; } ?> Additionally, you have your 'echo' in brackets in your tab.php file. I'd recommend removing the brackets. <?php echo ($row_Tab['Tab']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798721 Share on other sites More sharing options...
jandrews Posted April 1, 2009 Author Share Posted April 1, 2009 How do you get this 'tab' to come up? is it via the form with a button or... give me details on what you to. A 'tab' is a text field in the database. A tab is basically guitar music notation which is why it is in text. The tab shows on the page on the url variable '?track=example_song_title' This works fine for rows which have a tab in their tab field but for those that don't, I want to include the message, 'no tabs!'. Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798725 Share on other sites More sharing options...
JeanieTallis Posted April 1, 2009 Share Posted April 1, 2009 It is like a search bar? You type in lets say the song or band name click submit, and it comes up with the word 'Tab' ? Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798727 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 if (mysql_num_rows($results) > 0) { You want to see if there are 1 or more results. If there are then do the tabs, if not then no tabs were returned. 0 being returned by mysql_num_rows is a valid return value. Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798728 Share on other sites More sharing options...
JeanieTallis Posted April 1, 2009 Share Posted April 1, 2009 <?php if (mysql_num_rows($results) > 0) { include_once('tab.php'); } else { echo 'no tabs!'; } ?> (summerize of the two pieces of code given to make it easier) Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798735 Share on other sites More sharing options...
killah Posted April 1, 2009 Share Posted April 1, 2009 Is this file: <?php if(mysql_num_rows($results) > 0) include_once('tab.php'); else echo 'No Tabs!'; ?> .. being included or direct? If direct, maybe there is no resource ID for the $results to query. If so, add.. $results = mysql_query("SELECT `something` FROM `table` WHERE `clause` = '".mysql_real_escape_string($_POST['where_clause'])); If not. Please provide more code.[/code] Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798745 Share on other sites More sharing options...
jandrews Posted April 1, 2009 Author Share Posted April 1, 2009 Thanks for the help so far guys. So now i am using this code: <?php require_once('Connections/artists.php'); require_once('Connections/Tabs.php'); $colname_filename = "-1"; if (isset($_GET['track'])) { $colname_filename = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']); } $colname_artists = "-1"; if (isset($_GET['track'])) { $colname_artists = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']); } $colname_Tab = "-1"; if (isset($_GET['track'])) { $colname_Tab = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']); } mysql_select_db($database_Tabs, $Tabs); $query_Tab = sprintf("SELECT Tab FROM flamplayer_musics WHERE title_music LIKE '%%%s%%'", $colname_Tab); $Tab = mysql_query($query_Tab, $Tabs) or die(mysql_error()); $row_Tab = mysql_fetch_assoc($Tab); $totalRows_Tab = mysql_num_rows($Tab); if (mysql_num_rows($Tab) > 0) { include_once('tab.php'); } else { echo 'no tabs!'; } ?> ... and it displays the tabs for the rows that have tabs in their text field but for the ones without, the error message is not displaying. Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798753 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 Your logic is flawed. How are you pulling the data out of the database? Really what you are looking for or need is to test if there is a tab character in the field you are trying to find. If there is then display that tabs. stristr can help on the php side of things. On the MySQL side, I am unsure, but the problem does not lie within the code you have posted here. Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798754 Share on other sites More sharing options...
jandrews Posted April 1, 2009 Author Share Posted April 1, 2009 Yes sorry, here is the full code... <?php require_once('Connections/artists.php'); require_once('Connections/Tabs.php'); $colname_filename = "-1"; if (isset($_GET['track'])) { $colname_filename = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']); } $colname_artists = "-1"; if (isset($_GET['track'])) { $colname_artists = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']); } $colname_Tab = "-1"; if (isset($_GET['track'])) { $colname_Tab = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']); } mysql_select_db($database_Tabs, $Tabs); $query_Tab = sprintf("SELECT Tab FROM flamplayer_musics WHERE title_music LIKE '%%%s%%'", $colname_Tab); $Tab = mysql_query($query_Tab, $Tabs) or die(mysql_error()); $row_Tab = mysql_fetch_assoc($Tab); $totalRows_Tab = mysql_num_rows($Tab); if (mysql_num_rows($Tab) > 0) { <?php if (mysql_num_rows($Tab) > 0) { echo $row_Tab['Tab']; } else { echo 'no tabs!'; } ?> } else { echo 'no tabs!'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798777 Share on other sites More sharing options...
jandrews Posted April 1, 2009 Author Share Posted April 1, 2009 woops, wrong code. here it is... <?php require_once('Connections/artists.php'); require_once('Connections/Tabs.php'); $colname_filename = "-1"; if (isset($_GET['track'])) { $colname_filename = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']); } $colname_artists = "-1"; if (isset($_GET['track'])) { $colname_artists = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']); } $colname_Tab = "-1"; if (isset($_GET['track'])) { $colname_Tab = (get_magic_quotes_gpc()) ? $_GET['track'] : addslashes($_GET['track']); } mysql_select_db($database_Tabs, $Tabs); $query_Tab = sprintf("SELECT Tab FROM flamplayer_musics WHERE title_music LIKE '%%%s%%'", $colname_Tab); $Tab = mysql_query($query_Tab, $Tabs) or die(mysql_error()); $row_Tab = mysql_fetch_assoc($Tab); $totalRows_Tab = mysql_num_rows($Tab); if (mysql_num_rows($Tab) > 0) { echo $row_Tab['Tab']; } else { echo 'no tabs!'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798790 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 if (stristr($row_Tab, "\t") !== false) { echo $row_Tab['Tab']; } else { echo 'no tabs!'; } That should work I would think. See if that is what you want...I take it you are trying to see if the result has a tabbed character in it. Alternatively, this may work as well (although I am not sure): $query_Tab = sprintf("SELECT Tab FROM flamplayer_musics WHERE (title_music LIKE '%%%s%%' AND title_music LIKE '%%%s%%')", $colname_Tab, "\t"); Unsure of my sprintf usage, but yea. The query I would think would be the preferred method, but however you want it should work, as long as it does in deed work Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798797 Share on other sites More sharing options...
jandrews Posted April 1, 2009 Author Share Posted April 1, 2009 I take it you are trying to see if the result has a tabbed character in it. All I simply want the page to do is echo the error message if the 'Tab' field (which is a text field in my database) is empty ie doesn't contain any data. If however the 'Tab' field does contain text, it is echoed out. simples... Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798821 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 Oh well why did you not just say that if (!empty(trim($row_Tab['Tab']))) { echo $row_Tab['Tab']; }else{ echo 'no tabs!'; } I doubt the trim is needed, but yea. See if that works for you (and ignore the query up above revert it back to your original one). Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798824 Share on other sites More sharing options...
jandrews Posted April 1, 2009 Author Share Posted April 1, 2009 Wow! We got there in the end. ... and you are right, the trim() isn't needed, actually, it stops the code from working. Thanks Premiso for your time and help best wishes Quote Link to comment https://forums.phpfreaks.com/topic/152088-solved-how-do-i-code-an-if-statement-in-this-way/#findComment-798829 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.