Jump to content


Photo

[SOLVED]Could not connect to database


  • Please log in to reply
4 replies to this topic

#1 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 27 October 2006 - 11:44 AM

I created a script which works perfectly, i then started to modify it to use on anoth er webpasge and suddenly it cannot connect to the database...

Can someone help me find the problem please. The problematic query is in displayOneItem(); The query itself is good

Error message

Line 85: 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


Code
<?php

function Articlesfunction() {			
			function displayCategories() {
			global $dbc, $username, $type;
			
			$query = "SELECT * FROM site_categories WHERE cat_type=$type";
				$result = mysql_query ($query)or die("Problem with the query: $query on line:" . __LINE__ . "<br>" . mysql_error());
				while ($row = mysql_fetch_assoc ($result)) {
					/* display categories in a simple table */
					echo "<TABLE border=\"1\" width=\"580\">\n";
					
					$description = htmlentities ($row['description']);  
					$title = htmlentities ($row['cat_name']);
					echo "<TR><TD width=\"300\"><b>$title</b></TD>";				
								
					/* get number of articles */
					$comment_query = "SELECT count(*) FROM articles WHERE category_id={$row['cat_id']}";
					$comment_result = mysql_query ($comment_query);
					$comment_row = mysql_fetch_row($comment_result);
					
					/* display number of articls with link */
					echo "<TD><a href=\"{$_SERVER['PHP_SELF']}" .
						 "?action=show&cat_id={$row['cat_id']}\">Articles</a>" .
						 "($comment_row[0])</TD></TR>\n";
					echo "<tr><td colspan=\"2\">$description</td></tr>";
					
					/* finish up table*/
					echo "</TABLE>\n";
					echo "<BR>\n";
				}
			}
			
		function displayList($cat_id) {
				global $dbc, $type, $redirectpage;
				$dbc = mysql_connect ('localhost','root','admin');
				mysql_select_db ('UNC',$dbc);
				/* query for item */
				$query = "SELECT * FROM articles WHERE category_id='$cat_id'";
				$result = mysql_query ($query)or die("Problem with the query: $query on line:" . __LINE__ . "<br>" . mysql_error());
				
				/* if we get no results back, error out */
				if (mysql_num_rows ($result) == 0) {
					echo "There are no articles in this category\n";
					echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=all\">Back</a>\n";
					return;
				}
				echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=all\">Articles</a>\n";
				while ($row = mysql_fetch_assoc ($result)) {
					echo "<TABLE border=\"1\" width=\"580\">\n";
				
					/* easier to read variables and 
					 * striping out tags */
					$title = htmlentities ($row['title']);
					$author = htmlentities($row['author']);
					$postdate = htmlentities($row['postdate']);
					/* display the items */
					
					/* display number of articls with link */
					echo "<TD><a href=\"{$_SERVER['PHP_SELF']}" .
						 "?action=display&id={$row['article_id']}\">Article</a>" .
						 "</TD></TR>\n";
					echo "<TR><TD><b>$title</b></TD></TR>\n";
					echo "<TR><TD>$postdate<br /><i>Posted by $author </TD></TR>";				
					echo "</TABLE>\n";
					echo "<BR>\n";
				}				
		}
		
		function displayOneItem($id) {
				$dbc = mysql_connect ('localhost','root','admin');
				mysql_select_db ('UNC',$dbc);
				
				/* query for item */
				$query = "SELECT * FROM articles WHERE article_id='$id'";
				$result = mysql_query ($query) OR die ('Could not connect to MySQL: ' . mysql_error() );
				
				/* if we get no results back, error out */
				if (mysql_num_rows ($result) == 0) {
					echo "Bad article id\n";
					return;
				}
				$row = mysql_fetch_assoc($result);

				$comment_query = "SELECT cat_name FROM site_categories WHERE cat_id={$row['cat_id']}";
				$comment_result = mysql_query ($comment_query) OR die ('Line 85: ' . mysql_error() );
				$comment_row = mysql_fetch_row($comment_result);
				
	echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=all\">Articles</a>\n" . " - " . "<a href=\"{$_SERVER['PHP_SELF']}" ."?action=show&cat_id={$row['cat_id']}\">$comment_row[0]</a>\n";
				echo "<TABLE border=\"1\" width=\"580\">\n";
			
				/* easier to read variables and 
				 * striping out tags */
				$title = htmlentities ($row['title']);
				$news = nl2br (strip_tags ($row['articletext'], '<a><b><i><u>'));
				$author = htmlentities($row['author']);
				$postdate = ($row['postdate']);
				$downurl = ($row['down_name']);
				
				/* display the items */
				echo "<TR><TD><b>$title</b></TD></TR>\n";
				echo "<TR><TD>$news<br /><i>Posted by $author </TD></TR>";	
				echo "<tr><td>Get the file <a href=\"$downurl\">here.</a></td></tr>";			
				echo "</TABLE>\n";
				echo "<BR>\n";		
				displayComments($id);
		}
				
		function displayComments($id) {
				/* bring db connection variable into scope */
				global $dbc, $username, $IsLoggedIn, $user_id;
				
				/* query for comments */
				$query = "SELECT * FROM article_comments WHERE article_id=$id";
				$result = mysql_query ($query);
				echo "Comments:<BR>\n";
				
				/* display the all the comments */
				while ($row = mysql_fetch_assoc ($result)) {
					echo "<TABLE border=\"1\" width=\"580\">\n";
					
					$name = htmlentities ($row['name']);
					$comment = strip_tags ($row['comment'], '<a><b><i><u>');
					$comment = nl2br ($comment);
					$profileid = htmlentities ($row['user_id']);
					echo "<tr><td width=\"150\"><a href=\"/forums/profile.php?mode=viewprofile&u=" . $profileid . "\"><b>$name</b></a></td><td>$comment</td></tr>";				
					echo "</TABLE>\n";
					echo "<BR>\n";
				}
					if ($IsLoggedIn == 1) {	
						/* add a form where users can enter new comments */
						echo "<FORM action=\"{$_SERVER['PHP_SELF']}" .
							 "?action=addcomment&id=$id\" method=POST>\n";
						echo "<p>You are posting as " .$username. "<br />";
						echo "<TEXTAREA cols=\"40\" rows=\"5\" " .
							 "name=\"comment\"></TEXTAREA><BR>\n";
						echo "<input type=\"submit\" name=\"submit\" " .
							 "value=\"Add Comment\"\n";
						echo "</FORM>\n";
					} else {
						echo 'Please log-in to post a comment';
					}
		}
		
		function addComment($id) {
				global $dbc, $username, $user_id;
				$c = $_POST['comment'];
				/* insert the comment */
					$query = "INSERT INTO article_comments (article_id, name, comment, user_id) VALUES ('$id', '$username','$c', '$user_id')" OR die ('Could not connect to MySQL: ' . mysql_error() );
					mysql_query($query);				
					echo "Comment entered. Thanks!<BR>\n";
					echo "<a href=\"{$_SERVER['PHP_SELF']}" .
						 "?action=show&id=$id\">Back</a>\n";
		}
		

			
			echo "<CENTER>\n";
					switch($_GET['action']) {
						case 'display':
							displayOneItem($_GET['id']);
							break;
						case 'show':
							displayList($_GET['cat_id']);
							break;
						case 'all':
							displayCategories(1);
							break;
						case 'addcomment':
							addComment($_GET['id']);
						break;
						default:
							displayCategories();
}
			echo "</CENTER>\n";
} 
?>


#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 27 October 2006 - 12:10 PM

Line 85 runs a query. But the qeury has a bad syntax.
This part:
"WHERE cat_id={$row['cat_id']}"

Should be:
"WHERE cat_id='$row['cat_id']'"

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 27 October 2006 - 12:41 PM

It doesn't seem to like that.

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\webs\test\php\articlescript.php on line 85



#4 spfoonnewb

spfoonnewb
  • Members
  • PipPipPip
  • Advanced Member
  • 276 posts

Posted 27 October 2006 - 01:22 PM

Try

"WHERE cat_id='.$row['cat_id'].'"

Or

'WHERE cat_id='.$row['cat_id'].''

Or

'WHERE cat_id='.$row[cat_id].''

??

$comment_query = "SELECT cat_name FROM site_categories WHERE cat_id=".$row['cat_id']."";

??

$comment_query = "SELECT cat_name FROM site_categories WHERE cat_id=".$row[cat_id]."";

I was going to test them but the script doesn't do anything on its own :)

#5 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 27 October 2006 - 01:47 PM

Solved thx for the help guys




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users