SkyRanger Posted November 15, 2010 Share Posted November 15, 2010 Not exactly sure how to do this. What I need is when a person clicks on the link ie: page.php?nid=1 then it loads what needs to be loaded but if a user clicks and there is no nid ie: page.php?nid= Then it loads something else. if (nid==$nid){ echo "This is the data to show"; }else{ Show this data instead } Both if and else both show different mysql data. Hopefully I explained myself well. Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/ Share on other sites More sharing options...
TrueColors Posted November 15, 2010 Share Posted November 15, 2010 if($_GET['nid'] == $nid) { echo "this"; } else { echo "that"; } How about that? Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134276 Share on other sites More sharing options...
SkyRanger Posted November 15, 2010 Author Share Posted November 15, 2010 Ok, this is what I have and getting an error: if($_GET['nid'] == $nid) { mysql_connect("localhost", "", "") or die(mysql_error()); mysql_select_db("") or die(mysql_error()); $result = mysql_query("SELECT * FROM news where nid=$nid") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo "<strong>".$row['ntitle']."</strong>"; echo "<br><em>"; echo $row['ndate']; echo " by "; echo $row['npost']; echo "</em><br>"; echo $row['nnews']; echo "<br><br>"; echo $row['nmore']; } } else { mysql_connect("localhost", "", "") or die(mysql_error()); mysql_select_db("") or die(mysql_error()); $result = mysql_query("SELECT * FROM news") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo "<strong>".$row['ntitle']."</strong>"; echo "<br><em>"; echo $row['ndate']; echo " by "; echo $row['npost']; echo "</em><br>"; echo $row['nnews']; echo "<br><br>"; echo $row['nmore']; echo "<br><br>"; } } And the error I am getting is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134280 Share on other sites More sharing options...
TrueColors Posted November 15, 2010 Share Posted November 15, 2010 mysql_connect("localhost", "", "") or die(mysql_error()); mysql_select_db("") or die(mysql_error()); if($_GET['nid'] == $nid) { $result = mysql_query("SELECT * FROM news where nid='$nid'") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo "<strong>".$row['ntitle']."</strong>"; echo "<br><em>"; echo $row['ndate']; echo " by "; echo $row['npost']; echo "</em><br>"; echo $row['nnews']; echo "<br><br>"; echo $row['nmore']; } } else { $result = mysql_query("SELECT * FROM news") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo "<strong>".$row['ntitle']."</strong>"; echo "<br><em>"; echo $row['ndate']; echo " by "; echo $row['npost']; echo "</em><br>"; echo $row['nnews']; echo "<br><br>"; echo $row['nmore']; echo "<br><br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134285 Share on other sites More sharing options...
chintansshah Posted November 15, 2010 Share Posted November 15, 2010 or die() statement should continue after mysql_query. Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134304 Share on other sites More sharing options...
TrueColors Posted November 15, 2010 Share Posted November 15, 2010 or die() statement should continue after mysql_query. Incorrect, it worked the way he had it because although it's on a different line, it still worked. (Fact as he got an SQL error showing) The problem was probably because he was missing quotes around the variable being inserted into the query. Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134311 Share on other sites More sharing options...
SkyRanger Posted November 16, 2010 Author Share Posted November 16, 2010 Not sure if I am missing something really starting to drive me nuts...lol. The first part works great the if($_GET['nid'] == $nid) { but for some reason the else { is just giving me a blank page. Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134742 Share on other sites More sharing options...
Pikachu2000 Posted November 16, 2010 Share Posted November 16, 2010 Post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134743 Share on other sites More sharing options...
doni49 Posted November 16, 2010 Share Posted November 16, 2010 I just compared your MySQL code to what I've used on my own site (my code is working for me). Try putting single quotes around the $nid variable: mysql_query("SELECT * FROM news where nid='$nid'") Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134782 Share on other sites More sharing options...
SkyRanger Posted November 16, 2010 Author Share Posted November 16, 2010 Ok this is what I have if($_GET['nid'] == $nid) { $result = mysql_query("SELECT * FROM news where nid='$nid'") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo "<strong>".$row['ntitle']."</strong>"; echo "<br><em>"; echo $row['ndate']; echo " by "; echo $row['npost']; echo "</em><br>"; echo $row['nnews']; echo "<br><br>"; echo $row['nmore']; } } else { $result = mysql_query("SELECT * FROM news") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo "<strong>".$row['ntitle']."</strong>"; echo "<br><em>"; echo $row['ndate']; echo " by "; echo $row['npost']; echo "</em><br>"; echo $row['nnews']; echo "<br><br>"; echo $row['nmore']; } } As mentioned the if... works but the else... bring up just a blank page where there is suppose to be a list of entries Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134801 Share on other sites More sharing options...
Pikachu2000 Posted November 16, 2010 Share Posted November 16, 2010 Add some debugging echos to your code. } else { echo 'Entered else statement<br>.'; if($result = mysql_query("SELECT * FROM news") or die(mysql_error())) { echo 'Else query ran and returned ' . mysql_num_rows($result) . ' records.'; } while($row = mysql_fetch_array( $result )) { Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134808 Share on other sites More sharing options...
SkyRanger Posted November 16, 2010 Author Share Posted November 16, 2010 Add some debugging echos to your code. } else { echo 'Entered else statement<br>.'; if($result = mysql_query("SELECT * FROM news") or die(mysql_error())) { echo 'Else query ran and returned ' . mysql_num_rows($result) . ' records.'; } while($row = mysql_fetch_array( $result )) { Did this and still returned blank page Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134810 Share on other sites More sharing options...
Pikachu2000 Posted November 16, 2010 Share Posted November 16, 2010 Have you checked the 'View Source' in your browser to make sure it isn't getting malformed somehow? Is that all of the code in that script? Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134814 Share on other sites More sharing options...
SkyRanger Posted November 16, 2010 Author Share Posted November 16, 2010 Have you checked the 'View Source' in your browser to make sure it isn't getting malformed somehow? Is that all of the code in that script? Yeah checked the source code, nothing there. There is a problem with the }else { part of the code. Trying to figure it out. Pulled the debugging code out of the else statement and it worked perfectly. Now just to figure out what the problem is. Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134826 Share on other sites More sharing options...
laffin Posted November 16, 2010 Share Posted November 16, 2010 I don't see $nid being defined $nid=($t=intval($_GET['nid']))>0?$t:0; // make shure we have a valid whole number if($nid) { // post news } else { echo "Oopsie no news article by that id": } without $nid being defined the logic becomes reversed of what u wanted. Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134830 Share on other sites More sharing options...
SkyRanger Posted November 16, 2010 Author Share Posted November 16, 2010 Holy crap laffin. It works. Awesome, thank you very much you. Also to the rest of you again thank you. I actually learned allot from your guys posts. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/218694-if-then-problem/#findComment-1134836 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.