<?php include('./Connections/hostgator.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$maxRows_latest_news = 25;
$pageNum_latest_news = 0;
if (isset($_GET['pageNum_latest_news'])) {
  $pageNum_latest_news = $_GET['pageNum_latest_news'];
}
$startRow_latest_news = $pageNum_latest_news * $maxRows_latest_news;

mysql_select_db($database_hostgator, $hostgator);
$query_latest_news = "SELECT * FROM news ORDER BY `date` DESC";
$query_limit_latest_news = sprintf("%s LIMIT %d, %d", $query_latest_news, $startRow_latest_news, $maxRows_latest_news);
$latest_news = mysql_query($query_limit_latest_news, $hostgator) or die(mysql_error());
$row_latest_news = mysql_fetch_assoc($latest_news);

if (isset($_GET['totalRows_latest_news'])) {
  $totalRows_latest_news = $_GET['totalRows_latest_news'];
} else {
  $all_latest_news = mysql_query($query_latest_news);
  $totalRows_latest_news = mysql_num_rows($all_latest_news);
}
$totalPages_latest_news = ceil($totalRows_latest_news/$maxRows_latest_news)-1;

$maxRows_detailed_news = 1;
$pageNum_detailed_news = 0;
if (isset($_GET['pageNum_detailed_news'])) {
  $pageNum_detailed_news = $_GET['pageNum_detailed_news'];
}
$startRow_detailed_news = $pageNum_detailed_news * $maxRows_detailed_news;

$colname_detailed_news = "-1";
if (isset($_GET['id'])) {
  $colname_detailed_news = $_GET['id'];
}
mysql_select_db($database_hostgator, $hostgator);
$query_detailed_news = sprintf("SELECT * FROM news WHERE id = %s", GetSQLValueString($colname_detailed_news, "int"));
$query_limit_detailed_news = sprintf("%s LIMIT %d, %d", $query_detailed_news, $startRow_detailed_news, $maxRows_detailed_news);
$detailed_news = mysql_query($query_limit_detailed_news, $hostgator) or die(mysql_error());
$row_detailed_news = mysql_fetch_assoc($detailed_news);

if (isset($_GET['totalRows_detailed_news'])) {
  $totalRows_detailed_news = $_GET['totalRows_detailed_news'];
} else {
  $all_detailed_news = mysql_query($query_detailed_news);
  $totalRows_detailed_news = mysql_num_rows($all_detailed_news);
}
$totalPages_detailed_news = ceil($totalRows_detailed_news/$maxRows_detailed_news)-1;

mysql_select_db($database_hostgator, $hostgator);
$query_sub_cat_list = "SELECT * FROM sub_catergories ORDER BY cat_id, name ASC";
$scl = mysql_query($query_sub_cat_list, $hostgator) or die(mysql_error());
$row_sub_cat_list = mysql_fetch_assoc($scl);
$totalRows_sub_cat_list = mysql_num_rows($scl);
?>





<?php
function retrieve_listings($cat)
{
if(!isset($cat)) $cat = $_GET['cat'];

echo 'THIS IS WHERE THE RECORDS WILL BE DISPLAYED FOR CATERGORY '.$cat.'!!!!';
} ?>

<?php
function retrieve_cats($cat)
{
	while ($row_sub_cat_list = mysql_fetch_assoc($scl))
	{  
	echo $row_sub_cat_list['name'].'<br />';
	}
} ?>

<?php function revertTimeStamp($timestamp)
{
$year=substr($timestamp,0,4);
$month=substr($timestamp,4,2);
$day=substr($timestamp,6,2);
$hour=substr($timestamp,8,2);
$minute=substr($timestamp,10,2);
$second=substr($timestamp,12,2);
$newdate= date("d F Y", mktime($hour,$minute,$second,$month,$day,$year));
return($newdate); 
}
?>


<?php function assign_rand_value($num)
{
// accepts 1 - 36
  switch($num)
  {
    case "1":
     $rand_value = "a";
    break;
    case "2":
     $rand_value = "b";
    break;
    case "3":
     $rand_value = "c";
    break;
    case "4":
     $rand_value = "d";
    break;
    case "5":
     $rand_value = "e";
    break;
    case "6":
     $rand_value = "f";
    break;
    case "7":
     $rand_value = "g";
    break;
    case "8":
     $rand_value = "h";
    break;
    case "9":
     $rand_value = "i";
    break;
    case "10":
     $rand_value = "j";
    break;
    case "11":
     $rand_value = "k";
    break;
    case "12":
     $rand_value = "l";
    break;
    case "13":
     $rand_value = "m";
    break;
    case "14":
     $rand_value = "n";
    break;
    case "15":
     $rand_value = "o";
    break;
    case "16":
     $rand_value = "p";
    break;
    case "17":
     $rand_value = "q";
    break;
    case "18":
     $rand_value = "r";
    break;
    case "19":
     $rand_value = "s";
    break;
    case "20":
     $rand_value = "t";
    break;
    case "21":
     $rand_value = "u";
    break;
    case "22":
     $rand_value = "v";
    break;
    case "23":
     $rand_value = "w";
    break;
    case "24":
     $rand_value = "x";
    break;
    case "25":
     $rand_value = "y";
    break;
    case "26":
     $rand_value = "z";
    break;
    case "27":
     $rand_value = "0";
    break;
    case "28":
     $rand_value = "1";
    break;
    case "29":
     $rand_value = "2";
    break;
    case "30":
     $rand_value = "3";
    break;
    case "31":
     $rand_value = "4";
    break;
    case "32":
     $rand_value = "5";
    break;
    case "33":
     $rand_value = "6";
    break;
    case "34":
     $rand_value = "7";
    break;
    case "35":
     $rand_value = "8";
    break;
    case "36":
     $rand_value = "9";
    break;
  }
return $rand_value;
}

function get_rand_id($length)
{
  if($length>0) 
  { 
  $rand_id="";
   for($i=1; $i<=$length; $i++)
   {
   mt_srand((double)microtime() * 1000000);
   $num = mt_rand(1,36);
   $rand_id .= assign_rand_value($num);
   }
  }
return $rand_id;
} 

function get_rand_numbers($length)
{
  if($length>0) 
  { 
  $rand_id="";
   for($i=1; $i<=$length; $i++)
   {
   mt_srand((double)microtime() * 1000000);
   $num = mt_rand(27,36);
   $rand_id .= assign_rand_value($num);
   }
  }
return $rand_id;
} 

function newsletter()
{
$action = $_GET['action'];
if((isset($_GET['action']))&($action=="add")) { ?>

The email adding process has been completed.

<?php } 

if((isset($_GET['action']))&($action=="remove")) { ?>
The email removal process has been completed.
<?php }}

mysql_free_result($scl);
?>

The function retrieve_cats($cat) should display a list of every record in the database but it is giving me an error message of "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/whizzell/public_html/nath/inc/functions.php on line 100"

I don't know where i'm going wrong. I had this error earlier today with the same error but somehow fixed it. Can't find a solution this time.

I'm calling the function with <?php $cat = 1; retrieve_cats($cat);?>

Thanks in advance.

Will

Dani AI

Generated

Two likely causes and a simple path to confirm/fix them.

First: the warning means the value you passed to mysql_fetch_assoc() is not a valid result resource (it’s NULL, FALSE, or has already been freed). ’s quick tip (bringing the global into the function) will fix the common “variable not in scope” case, but there are other things to check too.

Quick checks (do these first)

  • Immediately after the query, make sure it actually returned a resource (and print mysql_error() if not).
  • Verify the table name (your query uses sub_catergories — confirm that spelling matches the database).
  • Confirm you haven’t called mysql_free_result($scl) or otherwise destroyed the resource before the function runs.
  • Remember you called mysql_fetch_assoc($scl) once at the top of the include: that advances the result pointer so a later loop may miss the first row unless you reset it.

Small debugging snippet to run right after the query:

if (!$scl) {
  trigger_error('sub_cat query failed: ' . mysql_error(), E_USER_WARNING);
  var_dump($scl); // shows FALSE or resource type
  return;
}

Recommended approaches

  • Best practice: don’t rely on globals. Pass the result into the function and validate it, then reset the pointer and loop:
    function retrieve_cats_from_result($result) {
    if (!is_resource($result)) return;
    mysql_data_seek($result, 0); // rewind if needed
    while ($row = mysql_fetch_assoc($result)) {
      echo htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8') . "<br>\n";
    }
    }

    Call it as retrieve_cats_from_result($scl);.

If you prefer the minimal change, add global $scl; inside the original function (as suggested) — but still check for a failed query or an earlier mysql_free_result call.

Longer term: plan to move off the deprecated mysql_* extension to mysqli or PDO.

Recommended Answers

All 2 Replies

Read the FAQ

Try replacing the retrieve_cats function with the following:

function retrieve_cats($cat)
{ global $scl;
	while ($row_sub_cat_list = mysql_fetch_assoc($scl))
	{  
	echo $row_sub_cat_list['name'].'<br />';
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.