Problem with "else" function.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kr9091
Forum Newbie
Posts: 8
Joined: Sun Sep 16, 2007 4:00 pm

Problem with "else" function.

Post by kr9091 »

I keep getting an error when I enter this code in.

Code: Select all

<?php
	require("config.php");
	
	if(isset($_GET['id']) == TRUE) {
		if(is_numeric($id) == FALSE) {
			$error = 1;
		}
		if($error == 1) {
			header("Location: " . $config_basedir . "/viewcat.php");
		}
		else {
			$validcat = $_GET['id'];
		}
	}
	else {
		$vaildcat = 0;
	}
	
	$sql = "SELECT * FROM categories";
	$result = mysql_query($sql);
	
	while($row = mysql_fetch_assoc($result)) {
		if($validcat == $row['id']) {
			echo "<strong>" . $row['cat'] . "</strong><br />";
			$entriessql = "SELECT * FROM entries WHERE cat_id = " . $validcat .
				" ORDER BY dateposted DESC;";
			$entriesres = mysql_query($entriessql);
			$numrows_entries = mysql_num_rows($entriesres);
			
			echo "<ul>";
			if($numrows_entries == 0) {
				echo "<li>" . date("D jS F Y g.iA", strtotime($entriesrow['dateposted'])) . " - <a href='viewentry.php?id=" . $entriesrow['id'] . "'>" . $entriesrow['subject'] ."</a></li>";
			}
		}
		echo "</ul>";
	}
	else {
		echo "<a href='viewcat.php?id=" . $row['id'] . "'>" . $row['cat'] . "</a><br />";
	}
}

	require("footer.php");
?>
The error says: "Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\New_Blog\viewcat.php on line 37"
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The last else is not associated with any if. It's attempting to associate with the while loop.
kr9091
Forum Newbie
Posts: 8
Joined: Sun Sep 16, 2007 4:00 pm

Post by kr9091 »

feyd wrote:The last else is not associated with any if. It's attempting to associate with the while loop.
Oh thanks haha. How stupid of me. Well now I have another problem...I'm getting an error message for the "mysql_fetch_assoc()" on line 21.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

kr9091 wrote: Oh thanks haha. How stupid of me. Well now I have another problem...I'm getting an error message for the "mysql_fetch_assoc()" on line 21.
That error would be.... ?

Try making Mysql show the error messages if it fails.

Code: Select all

$result = mysql_query($sql) or die('Mysql Query Failed. <br>' . mysql_error());
 
kr9091
Forum Newbie
Posts: 8
Joined: Sun Sep 16, 2007 4:00 pm

Post by kr9091 »

Zoxive wrote:
kr9091 wrote: Oh thanks haha. How stupid of me. Well now I have another problem...I'm getting an error message for the "mysql_fetch_assoc()" on line 21.
That error would be.... ?

Try making Mysql show the error messages if it fails.

Code: Select all

$result = mysql_query($sql) or die('Mysql Query Failed. <br>' . mysql_error());
 
The error reads: "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\New_Blog\viewcat.php on line 21" also the page is failing to connect to my MySQL server. I get the following error message: "Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\New_Blog\viewcat.php on line 20" But the other pages on the site query just fine.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

config.php contains code like
mysql_connect('', '', '', '');
mysql_select_db('');
?

Then please change it to

Code: Select all

mysql_connect('', '', '', '') or die(mysql_error());
mysql_select_db('') or die(mysql_error());
Post Reply