Page 1 of 1

Error help: not a valid mysql result

Posted: Mon Jul 29, 2002 7:17 am
by spear1976
Hi,

Back again with my joke DB.
Everything is working as it should, but i still get an error message when the user votes on a joke.
The error is:
Warning: Supplied argument is not a valid MySQL result resource in /var/www/html/demo/rating.php on line 50
The script still works, and the rating etc is updated in my DB, and all other scripts shows both grades and results without any problems.

Below you can find the source which handles the rating, in hopes that someone can find any errors in it. Line 50 is the while ($row = mysql_fetch_array($mysql_result))

PS: Sorry for bad language etc in the source, but it has been translated in a hurry, since it's original language is Norwegian.

Code: Select all

<?php

/* Included in funksjoenr.php is:
# Connect to the server
	$connection=mysql_pconnect("localhost","root","fjokt6a");
# Check connection
	if (!$connection) &#123;
		echo "Could not connect to the mySQL server!";
		exit;
	&#125;
# Select the database to use
	$db=mysql_select_db("vitser",$connection);
	
	# Check to see that switch is OK
	if (!$db) &#123;
		echo "Klarte ikke endring til databasen!";
		exit;
	&#125;
*/

    include ("funksjoner.php");
	$sql="SELECT * FROM vitser WHERE ID = '$id'";
	$mysql_result=mysql_query($sql,$connection);
	$num_rows=mysql_num_rows($mysql_result);		
		if	( $num_rows == 0 ) 
			&#123;
				echo "Sorry, no information";
			&#125; 
		else 
			&#123;
				while ($row = mysql_fetch_array($mysql_result)) 
					&#123; 
						$id=$row&#1111;"ID"];
						$FileName=$row&#1111;"FileName"];
						$Num_Votes=$row&#1111;"Num_Votes"];
						$Votes=$row&#1111;"Votes"];
			            $Rating = $row&#1111;"Rating"];
						$ID=$row&#1111;"ID"];
						$vedlegg=$row&#1111;"vedlegg"];
						$new_Votes=$Num_Votes+1;
						$Votes=$Votes+$Rate;
						$Rating=round(($Votes/$new_Votes),2); 
						$sql="UPDATE vitser SET Num_Votes='$new_Votes', Votes='$Votes', Rating='$Rating' where id='$id'";
						$mysql_result=  mysql_query($sql,  $connection) or die 
						("Trying to update DB - Could not execute query : $sql."  .  mysql_error());
					&#125; 
			

						if (!mysql_query($sql,$connection)) 
							&#123;
								print "$software - $version";
								print "<br><br>";
								print "$navbar";
								print "ERROR: The record could not be updated!!!<br> ";
								exit;
							&#125; else 
								&#123;					
							
									# Record added
									print "$software - $version";
									print "<br><br>";
									print "$navbar<br><br>";
									print "The joke now has a rating of <b>$Rating</b> after your vote.<br><br><br>";

								&#125;

					
	
			&#125;

mysql_close($connection);
?>

Posted: Mon Jul 29, 2002 8:18 am
by twigletmac
Try adjusting:

Code: Select all

$mysql_result=mysql_query($sql,$connection);
to

Code: Select all

$mysql_result = mysql_query($sql, $connection) or die(mysql_error().'<p>'.$sql.'</p>');
to see if you get a more useful error message.

Mac

Problem found

Posted: Mon Jul 29, 2002 12:11 pm
by spear1976
Hi,

Thank you very much for your reply. I got help, and found the rror. It was in the following line (i think, at least now everything works the way it should):

Code: Select all

$mysql_result= mysql_query($sql, $connection) or die 
            ("Trying to update DB - Could not execute query : $sql." . mysql_error());
My final problem (for now) is in the display of rated cards. The rating is stored as values 3 4 and 4. No decimal points, and this is OK.

However on my "Best rated" page, i would like to sort the jokes by the highest vote (which I'm doing now without problems), but i would like to make it so that the joke with the most votes *and* highest grade is displayed.

Does'nt help me much if 1 user rates a card as 5 star, and another cards has 4 but 456 votes.

Hope someone can help by viewing the below code, or fill me in in what i will need to look for:

Code: Select all

<?php

include ("funksjoner.php");
		

$x = 5; 
$sql="SELECT * FROM vitser ORDER BY Rating DESC LIMIT 0, $x";


$mysql_result=mysql_query($sql,$connection);
$num_rows=mysql_num_rows($mysql_result);		


if	( $num_rows == 0 ) &#123;
	echo "Sorry, no information found";
	&#125; else &#123;
		
/* Variables from funksjoner.php */
	echo "$software - $version";
	echo "<br><br>";
	echo "$navbar";
	
# VIS RESULTAT
echo "Displays the  best $x jokes (best rated:<br><hr><br>"; 
while ($row = mysql_fetch_array($mysql_result)) &#123; 
			$tittel=$row&#1111;"tittel"];
			$kategori=$row&#1111;"kategori"];
			$gittav=$row&#1111;"gittav"];
			$epost=$row&#1111;"epost"];
           		$innhold = nl2br($row&#1111;"innhold"]);
			$ID=$row&#1111;"ID"];
			$counter=$row&#1111;"counter"];
			$vedlegg=$row&#1111;"vedlegg"];
			$Rating=$row&#1111;"Rating"];
			$Num_Votes=$row&#1111;"Num_Votes"];


	echo "JokeNR: $ID - <b>$tittel</b> - (<i>Av: <a href="mailto:$epost">$gittav</A>, read $counter times - Rated: $Rating stars - Number of votes: $Num_Votes</I>)<br><br>";
/* insert the joke */

	echo "$innhold";
	echo "<br><hr><br>";

	&#125;
	&#125; # END ELSE
	
	mysql_close($connection);
	?>