Page 1 of 1

function not recognizing variable passing in querystring

Posted: Mon Jun 23, 2003 9:50 pm
by decoy1
Hi,

I am having a helluva time with some pretty simple bits of code and wanted someone to look over it and give me a suggestion. It looks long but it is very straightforward.

I have a couple of functions which pull info from a database, and a couple that display that info. I can get one set to work but not the other. The pair that work are called like so..

Code: Select all

$cat_array = get_categories();
display_categories($cat_array);
and their repsective code...

Code: Select all

function get_categories()
{
   $query = "select catid, cat_name
             from link_cat"; 
   $result = @mysql_query($query);
   if(!$result)
     return false;
   $num_cats = @mysql_num_rows($result);
   if($num_cats ==0)
      return false;  
   $result = db_result_to_array($result);
   return $result; 
}
and
function display_categories($cat_array)
{
  if(!is_array($cat_array))
  {
     echo '<br>No categories currently available<br>';
     return;
  }
  foreach($cat_array as $row)
  {
    $url = 'subpages/show_cat.php?catid=' .($row['catid']). ' target=content';
    //$url = 'includes/show_categories.php?catid=' .($row['catid']). ' target=content';    
    $title = $row["cat_name"]; 
    do_html_url($url, $title); // This function justs does a little formatting
  }
}
This works nice. All the links are now passing their respective 'catid'. Okay, now when I click the links it points to this...

Code: Select all

$links = getLinks($catid);
displayLinks($links);
and their repsective code...

Code: Select all

function getLinks($catid)
{
   $catid = $_GET['catid'];
   if(!$catid || $catid=="")
     return false;
   $query = "select * from links where catid='$catid'";
   $result = @mysql_query($query);
   if(!$result)
     return false;
   $num_links = @mysql_num_rows($result);
   if($num_links == 0)
      return false;
   $result = db_result_to_array($result);
   return $result;
}
and
function displayLinks($link_array)
{
  if(!is_array($link_array))
  {
    echo '<br>No links currently available in this category<br>';
  }
  else
  {
    foreach($link_array as $row)
    {
      $url = 'show_book.php?linkid=' .($row['linkid']). ' target=content';
      $title = $row["catname"];
      do_html_url($url, $title);
    }
  }
}
I always get "No categories currently available", which is not true. There are three. I've done queries in MySqlFront and they all pop up.

The only difference in get_categories and getLinks is that getLinks accepts the catid as a parameter which was passed to it in display_categories. I can see that the catid is being passed when I run the cursor over the links, but getLinks does NOT see it. I have plugged in so many other little functions to test whether catid has a value and it doesn't. It does not make any sense to me whatsoever.

I welcome all suggestions gladly :)
Thanks

Posted: Tue Jun 24, 2003 3:41 am
by delorian
Is catid a number value. If so try change the query:

Code: Select all

$query = "select * from links where catid=".intval($catid); 
 // intval - is just for better protection;
In getLinks function.

And also I would change:

Code: Select all

$links = getLinks($_GET['catid']); 
displayLinks($links);
And erase that $catid=$_GET['catid'] line in the function.

Posted: Tue Jun 24, 2003 12:53 pm
by decoy1
Hi,
Thanks for responding. I've already tried those out without any results. I've even passed a number as a string directly to the getLinks function (as an existing catid) and it still doesn't see any links under the particular categories.

I use MySqlFront as a gui interface to mysql and I can run direct queries like "select * from links where catid = '1'; and boom....they all pop up.`

The catid is being passed to a framed page where the code is included. I thought that might be the problem, but I switched things around and called the function directly to the page with the code and the same thing happens. BTW in my original post I stated that I was getting the message "no categories available" when I meant "no links available in this category". The categories show up fine.

I can set this up on a live domain if that would help.

echo

Posted: Tue Jun 24, 2003 1:25 pm
by phpScott
Have you tried to echo out what $result is in your getLinks($catid) function to see what is being generated.
Also try echoing your sql statement and running it agianst the db to see if there is an error there.

phpScott

Posted: Tue Jun 24, 2003 2:00 pm
by decoy1
Hey Scott,

I have tried both of your suggestions. $result is empty and running the query returns results. I've dealt with things much more complex as this but something is getting past me. The url is http://www.karenwilliamson.com. The links I am working with are on the left side.