Jump to content

Help with troubleshooting


Perad

Recommended Posts

The script works and does everything i want it to do. Unfortunately it also does something else and final page is showing a database error for something it shouldn't even be calling.

Basically the script contains 3 functions which take actions from a switch statement. It all works but but on the last page there is this error.

[quote]Problem with the query: SELECT * FROM downloads WHERE cat_id= on line:64
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]

The script for some reason is doing a database query from displayList(); when it shouldn't be. Could someone help me make this stop please. Thanks

[code]<?php
$type = 1;
//sorting out redirects
$redirectpage = NULL;
if ($type == 1) {
$redirectpage = 'downloads.php';
} else if ($type == 2) {
$redirectpage = 'articles.php';
} else if ($type == 3) {
$redirectpage = 'demos.php';
}
//End of redirects

// type
if ($type == 1) {
$typetext = 'downloads';
} else if ($type == 2) {
$typetext = 'articles';
} else if ($type == 3) {
$typetext == 'demos';
}

//End Type


function displayCategories() {
$dbc = mysql_connect ('localhost','root','admin');
mysql_select_db ('UNC',$dbc);

global $dbc, $username;
$type = 1;

$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";
 
$title = htmlentities ($row['cat_name']);
echo "<TR><TD width=\"300\"><b>$title</b></TD>";

/* get number of articles */
$comment_query = "SELECT count(*) FROM downloads WHERE cat_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";

/* 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 downloads WHERE cat_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 "Bad news id\n";
return;
}
while ($row = mysql_fetch_assoc ($result)) {
echo "<TABLE border=\"1\" width=\"580\">\n";

/* easier to read variables and
* striping out tags */
$title = htmlentities ($row['down_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['down_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";
}
/* now show the comments */

}

function displayOneItem($id) {
$dbc = mysql_connect ('localhost','root','admin');
mysql_select_db ('UNC',$dbc);
global $dbc;

/* query for item */
$query = "SELECT * FROM downloads WHERE down_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 news id\n";
return;
}
$row = mysql_fetch_assoc($result);
echo "<TABLE border=\"1\" width=\"580\">\n";

/* easier to read variables and
* striping out tags */
$title = htmlentities ($row['down_title']);
$news = nl2br (strip_tags ($row['comments'], '<a><b><i><u>'));
$author = htmlentities($row['author']);
$postdate = ($row['postdate']);

/* display the items */
echo "<TR><TD><b>$title</b></TD></TR>\n";
echo "<TR><TD>$news<br /><i>Posted by $author </TD></TR>";
echo "</TABLE>\n";
echo "<BR>\n";

/* now show the comments */
}

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

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/25165-help-with-troubleshooting/
Share on other sites

To make this clearer...

displayCategories shows the following.
Category 1
Description

Category 2
Description

displayList shows
Article 1
Article 2
Article 3

displayOneItem shows
Article Title
Text

The problem is with displayOneItem

It shows
-------------------
|Article Title
|------------------
|Content
|
|------------------

There are no articles in this category Back


As you can see "There are no articles in this category Back" Shouldn't be there as this is from the displaylist function. I was hoping someone could help remove this.

My updated script is here

[code]<?php
$type = 1;
$dbc = mysql_connect ('localhost','root','admin');
mysql_select_db ('UNC',$dbc);

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 downloads WHERE cat_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 downloads WHERE cat_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;
}
while ($row = mysql_fetch_assoc ($result)) {
echo "<TABLE border=\"1\" width=\"580\">\n";

/* easier to read variables and
* striping out tags */
$title = htmlentities ($row['down_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['down_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);
global $dbc;

/* query for item */
$query = "SELECT * FROM downloads WHERE down_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);
echo "<TABLE border=\"1\" width=\"580\">\n";

/* easier to read variables and
* striping out tags */
$title = htmlentities ($row['down_title']);
$news = nl2br (strip_tags ($row['comments'], '<a><b><i><u>'));
$author = htmlentities($row['author']);
$postdate = ($row['postdate']);

/* display the items */
echo "<TR><TD><b>$title</b></TD></TR>\n";
echo "<TR><TD>$news<br /><i>Posted by $author </TD></TR>";
echo "</TABLE>\n";
echo "<BR>\n";

}

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

?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.