Jump to content

[SOLVED]Could not connect to database


Perad

Recommended Posts

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
[quote]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[/quote]

Code
[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";
}
?>[/code]
Link to comment
Share on other sites

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 :)
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.