...

View Full Version : MySQL Error in class



vern
09-25-2004, 05:56 AM
Hi, I have a class that returns the category name when given the category ID. It works fine, but I get the following MySQL warning in my logs ...



[client 192.168.0.2] PHP Warning: mysql_result() [<a href='function.mysql-result'>function.mysql-result</a>]: Unable to jump to row 0 on MySQL result index 64 in /home/vern/public_html/icommerce/etc/class.php on line 38


The class is:



function checkCat($category_id, $url_yes)
{
require("config.php");
$query = "SELECT * FROM categories WHERE cat_id = $category_id";
$result = mysql_query($query, $connection);
$cat_name = mysql_result($result, 0, "cat_name");
if ($url_yes == 1)
$cat_name = '<a href="index-dev.php?cat='.$category_id.'">'.$cat_name.'</a>';
return $cat_name;
}

marek_mar
09-25-2004, 10:57 AM
Well It's a function like the declaration suggests (function checkCat($category_id, $url_yes)) not a class.

vern
09-25-2004, 02:12 PM
A function in a class.

AaronW
09-25-2004, 02:33 PM
The thing about mysql_result () is that you'll get that error when your query doesn't return any rows. That is, mysql_result () needs at least one row to be returned, otherwise it can't grab the field (since your query didn't find anything). You'll get that error when your category ID doesn't exist in the database, but it gets passed to the function anyway.

To avoid the error, try this:


function checkCat($category_id, $url_yes)
{
require ("config.php");
$query = "SELECT cat_name FROM categories WHERE cat_id=$category_id";
$row = mysql_fetch_row (mysql_query ($query, $connection));
if ($cat_name = $row[0])
{
if ($url_yes == 1)
$cat_name = '<a href="index-dev.php?cat='.$category_id.'">'.$cat_name.'</a>';
return $cat_name;
}
else return false;
}

vern
09-25-2004, 08:56 PM
AaronW, thanks!

That fixed my problem and I only needed to tweak it a bit. Thanks for the explanation also. Great help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum