Page 1 of 1

mysql_fetch_assoc supplied argument not valid MySQL [SOLVED]

Posted: Sun Sep 16, 2007 4:09 pm
by kr9091
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am having trouble getting this code to display correctly. I recieve this error when I try to preview the page in firefox: "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\New_Blog\viewentry.php on line 34." I have provided the code from the entire page and would appreciate it if someone could point out what I am doing wrong. Thanks.

Code: Select all

<?php
require("config.php");

if(isset($_GET['id']) == TRUE) {
	if(is_numeric($_GET['id']) == FALSE) {
		$error = 1;
	}
	
	if($error == 1) {
		header("Location: " . $config_basedir);
	}
	else {
		$validentry = $_GET['id'];
	}
}
else {
	$validentry = 0;
}

require("header.php");
if($validentry == 0) {
	$sql = "SELECT entries.*, categories.cat FROM entries, categories " . 
			"WHERE entries.cat_id = categories.id " .
			"ORDER BY dateposted DESC " .
			" LIMIT 1;";
}
else {
	$sql = "SELECT entries.*, categories.cat FROM entries, categories " .
			"WHERE entries.cat_id = categories.id
	AND entries.id = " . $validentry . 
			"ORDER BY dateposted DESC " . "LIMIT 1;";
}
	$result = mysql_query($sql);
	$row = mysql_fetch_assoc($result);
	echo "<h2>" . $row['subject'] . "</h2><br />";
	echo "<i>In <a href='viewcat.php?id=" . $row['cat_id'] ."'>" . $row['cat'] . 
		"</a> - Posted on " . date("D jS F Y g:iA", 
	strtotime($row['dateposted'])) .
		"</i>";
	echo "<p>";
	echo nl2br($row['body']);
	echo "</p>";
	
	$commsql = "SELECT name FROM comments WHERE blog_id = " . $validentry . 
				" ORDER BY dateposted;";
	$commresult = mysql_query($commsql);
	$numrows_comm = mysql_num_rows($commresult);
	if($numrows_comm == 0) {
		echo"(<strong>" . $numrows_comm . "</strong>) comments : ";
		$i = 1;
		while($commrow = mysql_fetch_assoc($commresult)) {
			echo "<a href='viewentry.php?id=" . $row['id'] ."#comment" . $i .
					"'>" . $commrow['name'] . "</a> ";
			$i++;
		}
	}
 
?>
:D


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Sep 16, 2007 4:18 pm
by s.dot

Code: Select all

$result = mysql_query($sql);
:arrow:

Code: Select all

$result = mysql_query($sql) or die(mysql_error());
Your query is not returning a result set. This could be caused by a query error, or mysql returning 0 rows. If there is no query error, check the mysql_num_rows($result) before fetching the $row.

Posted: Sun Sep 16, 2007 4:27 pm
by volka
I also suggest to edit C:\xampp\apache\bin\php.ini
search for
; Trace mode. When trace_mode is active (=On), warnings for table/index scans and
; SQL-Errors will be displayed.
mysql.trace_mode = Off
and change it to mysql.trace_mode = On
That doesn't suffice as error handling but at least you will get a warning message.

Posted: Sun Sep 16, 2007 4:31 pm
by kr9091
Ok so I entered this code:

Code: Select all

$result = mysql_query($sql) or die(mysql_error());
and it returned this error message: "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 'BY dateposted DESC LIMIT 1' at line 2"

Posted: Sun Sep 16, 2007 4:35 pm
by volka
kr9091 wrote:Ok so I entered this code:

Code: Select all

$result = mysql_query($sql) or die(mysql_error());
and it returned this error message: "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 'BY dateposted DESC LIMIT 1' at line 2"
please "extend" the error handling to

Code: Select all

$result = mysql_query($sql) or die(htmlentities(mysql_error().': "'.$sql.'"'));

Posted: Sun Sep 16, 2007 4:41 pm
by kr9091
volka wrote:
kr9091 wrote:Ok so I entered this code:

Code: Select all

$result = mysql_query($sql) or die(mysql_error());
and it returned this error message: "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 'BY dateposted DESC LIMIT 1' at line 2"
please "extend" the error handling to

Code: Select all

$result = mysql_query($sql) or die(htmlentities(mysql_error().': "'.$sql.'"'));
Ok now the error reads like this:

"Warning: mysql_query() [http://www.mysql.com/doc]: 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 'BY dateposted DESC LIMIT 1' at line 2 in C:\xampp\htdocs\New_Blog\viewentry.php on line 34
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 'BY dateposted DESC LIMIT 1' at line 2: "SELECT entries.*, categories.cat FROM entries, categories WHERE entries.cat_id = categories.id AND entries.id = 4ORDER BY dateposted DESC LIMIT 1;"

Posted: Sun Sep 16, 2007 4:43 pm
by superdezign
There's no space between "entries.id = 4" and "ORDER BY." Fix it.

Posted: Sun Sep 16, 2007 4:52 pm
by kr9091
superdezign wrote:There's no space between "entries.id = 4" and "ORDER BY." Fix it.
Ok, awesome that worked. Thanks so for your help guys. I appreciate it.