Jump to content

If then problem


SkyRanger

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";
}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 )) {

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.