Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › PHP: Variable name as argument in function
New Posts  All Forums:Forum Nav:

PHP: Variable name as argument in function - Page 4

post #31 of 35
It still doesn't appear that you've defined field_12. Fix that next.
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #32 of 35
Thread Starter 
field_12 isn't anything that's supposed to be defined. it's part of the query (implied as a string value into the function) and has already been put in quotation marks, but be it with or without; the result remains the same.

It's like this ever since I changed it:
Code:

            else {

                if(!stat_null("field_12", $conn)){
                    fetch_stat("field_12", "xp", $conn);
                    echo $xp . " xp.";
                }
            } 

Edited by RevZ - 5/7/11 at 10:47am
post #33 of 35
OK, well are you sure there are actually any rows being returned from the query? That's the error that I would expect if the query returned nothing. Did you check mysql_num_rows()?
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #34 of 35
Thread Starter 
That's the thing, even if i replace the variable part of the query with something static, the output is null (not even a query output, just null), as if there never was any output (as if it never even executed).

However, if replacing the $field in the query with for example field_11 or field_12, the query is a literal copy/paste from the working script. Like I said, both the query and the parts of the script that process the output are literally taken from a working script.

It seems to have to do with the script being separated, because this code from my old script works perfectly:

Code:

$xp = mysql_query("SELECT field_12 FROM data_pfields_content WHERE member_id='" . $userno . "'")
or die(mysql_error());

$output2 = mysql_result($xp, 0); 
If I were to print $output2 it would give me the value for my user number, set in the cookie it fetches.

EDIT: I just noticed that all the variables defined in dat_commoncode.php do nothing; it's as if they're disappearing...
print $userno doesn't yield me the usernumber, and the user/pass/database from the connection details are all empty when trying to print them from inside any of the functions..

Functions not seeing connection details (which should be globally available because all functions need them basically), perhaps?
Edited by RevZ - 5/7/11 at 11:16am
post #35 of 35
Thread Starter 
Sorry for the doublepost, but I seem to have made the base of it work!

All I did was to alter some bits:
test_newscript.php:
Code:

            else {
                if(stat_null("field_12", $conn)){
                    $xp1 = fetch_stat("field_12", $conn);
                    echo $xp1 . " xp.";
                }
            } 
dat_commoncode.php:
Code:

    function stat_null($field, $conn) {
        global $userno;
        $stat1 = mysql_query("SELECT " . $field . " FROM data_pfields_content WHERE member_id='" . $userno . "'", $conn)
        or print(mysql_error());
        //print_r (mysql_num_rows($stat1));
        $stat2 = mysql_result($stat1, 0);
                        
        if (is_null($stat2)) {
            //echo "1";
            return false;
        }
        else {
            //echo "0";
            return true;
        }
    }
    
    function fetch_stat($field, $conn) {
        global $userno;
        $fetch = mysql_query("SELECT " . $field . " FROM data_pfields_content WHERE member_id='" . $userno . "'", $conn)
        or die(mysql_error());
                
        $output = mysql_result($fetch, 0);
        return $output;
    }
    
    function zero_stat($field, $conn) {
        global $userno;
        $fetch = mysql_query("UPDATE data_pfields_content SET " . $field . " = '0' WHERE member_id='" . $userno . "'", $conn)
        or die(mysql_error());
    } 
In the end, the topmost vars in the common code file were out of scope in the functions, so I had to make global the only one that needs direct access from within the functions, and it works. Whether or not it's a good way of doing that is another thing, but atleast it works. Also, I had to change the way the function spits out a variable, by making it return the variable instead of outputting a separate variable.

I hope it'll work this way for the remaining functions that I'm going to have to put into the common code file; that way I can finally start making my modules and not have to worry about those database calling functions as much anymore.
Edited by RevZ - 5/8/11 at 4:57am
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › PHP: Variable name as argument in function