Jump to content


PHP help with mysq_fetch_array error


2 replies to this topic

#1 nygorn

    Young Padawan

  • Members
  • Pip
  • 53 posts
  • Gender:Male
  • Location:Sweden

Posted 30 December 2008 - 03:38 PM

I'm trying to make my first php script but i've ran on a problem that i cant solve.
<?php
include "config.php";
include "connect_database.php";

function shortenText($input,$size){
if(strlen($input) > $size){
return substr($input,0,$size).'...';
} else {
return $input;
}
}
if (isset($_GET['id'])) {
$id = $_GET['id'];
}
else
$result = mysql_query("SELECT * FROM communitynews") or die(mysql_error());
while($row = mysql_fetch_array( $result ))
{

	echo "<h3><i class='leftness'>Posted on : ";
	echo $row['date'];
	echo " : <b>";
	echo $row['rubrik'];
	echo "</b></i> <em>";
	echo $row['cat'];
	echo "</em></h3>
		<div class='content'>
			<div class='content_bottom'><div class='left_box'>
				<div class='avi'><img src='i/members/avtars/";
	echo $row['img'];
	echo ".jpg' alt='' /></div>
				<div class='left_box_right'>
				USER <a href=''>Eon</a><br />
				RANK Chief<br />
				<img src='i/online.jpg' alt='' />
				REP 98<br />
				WEBSITE <a href='http://www.halotrilogy.org'>teamvirtue.com</a>
				</div>
				
			</div>
				<div class='content_text'><p>";
	echo shortenText($row['content'], 5);
	echo "</p>
				</div>
				<div class='clear'></div>
			</div>
			<div class='clear'></div>
		</div>
		<div class='content_footer'>
			<a class='read' href='test.php?id=";
	echo $row['id'];
	echo "'></a>
			<div class='right'>
			</div>
		</div>";
}


$result = mysql_query("SELECT * FROM communitynews WHERE id='$id' ") or die(mysql_error());
while($row = mysql_fetch_array( $result ))

{

	echo "<h3><i class='leftness'>Posted on : ";
	echo $row['date'];
	echo " : <b>";
	echo $row['rubrik'];
	echo "</b></i> <em>";
	echo $row['cat'];
	echo "</em></h3>
		<div class='content'>
			<div class='content_bottom'><div class='left_box'>
				<div class='avi'><img src='i/members/avtars/";
	echo $row['img'];
	echo ".jpg' alt='' /></div>
				<div class='left_box_right'>
				USER <a href=''>Eon</a><br />
				RANK Chief<br />
				<img src='i/online.jpg' alt='' />
				REP 98<br />
				WEBSITE <a href='http://www.halotrilogy.org'>teamvirtue.com</a>
				</div>
				
			</div>
				<div class='content_text'><p>";
	echo $row['content'];
	echo "</p>
				</div>
				<div class='clear'></div>
			</div>
			<div class='clear'></div>
		</div>
		<div class='content_footer'>
			<a class='read' href='test.php?id=";
	echo $row['id'];
	echo "'></a>
			<div class='right'>
			</div>
		</div>";
}
?>

Gives error:

Quote

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\server\sites\empis.jarbo\train\test.php on line 142
when i go in to ?id=1

#2 Rcty

    Young Padawan

  • Members
  • Pip
  • 3 posts
  • Gender:Male

Posted 30 December 2008 - 04:26 PM

I believe the problem lies within the fact that you don't have any brackets for the else statement.

Your current if statement is this:
if (isset($_GET['id'])) {
$id = $_GET['id'];
}

Your current else statement is this:
else
$result = mysql_query("SELECT * FROM communitynews") or die(mysql_error());

Everything else below that is running regardless of the if/else.

You can have an if/else statement without brackets, but PHP will only will read what's on the rest of the line and the line below it. The first while statement is running regardless of what happens, and because of this, when you have an ID set, PHP is taking out the else and $result variable, but trying to run the loop. However, because $result is then not set, it's trying to run mysql_fetch_array() on an empty variable. Put brackets around the code the else statement is supposed to trigger, and you should be good.

#3 nygorn

    Young Padawan

  • Members
  • Pip
  • 53 posts
  • Gender:Male
  • Location:Sweden

Posted 30 December 2008 - 04:35 PM

Well, thanks, it was solved now :angrylooking:
Thank also for explaining so good so me with my poor english and lack of php skills understood :)
Cheers, and thanks allot!
Happy new year too :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users