mysql_fetch_assoc supplied argument not valid MySQL [SOLVED]

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

mysql_fetch_assoc supplied argument not valid MySQL [SOLVED]

Post 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]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
kr9091
Forum Newbie
Posts: 8
Joined: Sun Sep 16, 2007 4:00 pm

Post 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"
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.'"'));
kr9091
Forum Newbie
Posts: 8
Joined: Sun Sep 16, 2007 4:00 pm

Post 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;"
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

There's no space between "entries.id = 4" and "ORDER BY." Fix it.
kr9091
Forum Newbie
Posts: 8
Joined: Sun Sep 16, 2007 4:00 pm

Post 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.
Post Reply