Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Author Topic: Function works in one location but not in another  (Read 1758 times)

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6020
  • Semantically Challenged
    • snews.ca
Function works in one location but not in another
« on: August 19, 2008, 03:38:39 am »

The Project:
I am working on a function and dbase table that will (eventually) be used by the Admin to enter & edit a group of configuration options for a calendar module project.

Code: [Select]
<?php

function show_options() { 
$user_prefix 'mycal_';
$query "SELECT * FROM ".$user_prefix."options WHERE identifier = 1";
$result mysql_query($query);
while ($r mysql_fetch_array($result)) {
                
$hover_color $r['month_hover_color'];
                
$day_color $r['month_day_color'];
                
$today_color $r['month_today_color'];
                
$cal_title $r['calendar_title'];
        echo 
'<b>TITLE:</b> '.$cal_title.'<br /><b>HOVER COLOR:</b> '.$hover_color.'<br /><b>DAY COLOR:</b> '.$day_color.'<br /><b>TODAYS COLOR:</b> '.$today_color.'';
}

?>


This function works just fine when I place it in either of these locations:
     - snews.php file,
     - a root/mods/testfuncs.php file
     - a root/mycalendar/options.php file
and displays the values from the options table as it should.

However... when I use the script contained within the function (excluding the opener & closer) within another existing function... it returns:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in .......... on line 122
Line 122 is... while ($r = mysql_fetch_array($result)) {

I cannot understand why it works OK as a stand-alone function but not as a script within another function (that also works OK without it)
If anyone might have an idea why this occurs... well sir... I be much obliged for the solution.  :P
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

Joost

  • Guest
Re: Function works in one location but not in another
« Reply #1 on: August 19, 2008, 04:16:44 am »

@Doug,

The warning is caused by the query. Line 122 is getting an error message coming from the database.
It might not recognize the table that's requested (mycal_options). Is that table in the same db?
Logged

funlw65

  • Hero Member
  • *****
  • Karma: 96
  • Posts: 771
    • Country Lab
Re: Function works in one location but not in another
« Reply #2 on: August 19, 2008, 05:02:01 am »

Maybe:
- $user_prefix was not defined inside host function
- identifier is not a field of table (or is miss typed) 
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6020
  • Semantically Challenged
    • snews.ca
Re: Function works in one location but not in another
« Reply #3 on: August 19, 2008, 04:46:33 pm »

Thanks for your suggestions.
The thing is... the script runs fine and displays the requested values from mycal_options table (in the same dbase) when it is used as a full stand-alone function in several file locations as long as those files are "included" with snews.php.

More about the project:
I am attempting to convert a calendar script that was developed as a phpNuke module. I am interested in this one because it is very versatile, in that it supports recurring and future events posting and includes an Admin panel where the Admin can add & edit several options (in the options table) that (among other things) control background, text and link colors for the calendar month-view panel.

phpNuke modules are not simple to begin with, and they must interact with database functions that determine what dbase type is being used (and other system checks). I am trying to rewrite the functions module-wide to make them independent of the phpNuke engine.

Also... I was getting the same error message in two other function files with their query structures un-modified (still in default form)... and these two were also connecting to the same mycal_options table.

At this point, I do have the month-view (front-end) displaying fine. For the time being, I replaced the options table queries (don't really need the options table) with global variable/values.

I realize I will get into all sorts of issues as I proceed. I'll just rewrite all of the functions to remove their default Nuke-function connections... one at a time. :)

Project Status: Dumped... abandoned. Not a practical pursuit at all. Nuke uses a whack of db function files to generate query classes (I think) and converts them to run on several dbase types (msaccess, mssql, mssql-odbc, mysql, mysql4, oracle, postres7 & sqlite)... so the queries are not within most of the functions.
« Last Edit: August 19, 2008, 09:05:37 pm by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU