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 3

post #21 of 35
Who knows? You'll have to share the rest of your code.
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 #22 of 35
With no information can't say, but I'd start with, what's the state of the query/connection/other-objects in the working function before the call, and compare that to the broken one. The dangers of cut-n-paste code.
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
post #23 of 35
Thread Starter 
Okay.

I did strip my code of all functions that are going to be in there in the end, because this was a test file to begin with. Keep that in mind, but I need this to work for all the other functions to work.

I'll post up the code as it is at the moment which is how I want it to be (the connection crap was supposed to sit in the same file as the other commonly used code, but to eliminate that as being the issue I separated it; didn't fix anything.):

test_newscript.php (the main file)
Code:

<html>

    <head>
        <title>
            testing stuff in here!
        </title>
    </head>
    <body>
        <?php
            require 'dat_dbconn.php';
            require 'dat_commoncode.php';    

            if ($userno == '0') {
                echo "USER NOT LOGGED IN!";
            }
            
            else {
                if(stat_null(field_12) == '0'){
                    fetch_stat(field_12, xp);
                    echo $xp . " xp.";
                }
                mysql_close($conn);
            }
        ?> 
    </body>
</html>
dat_commoncode.php:
Code:

<?php

    function stat_null($field) {
        $stat1 = mysql_query("SELECT $field FROM data_pfields_content WHERE member_id='" . $userno . "'")
        or print(mysql_error());
        $stat2 = mysql_result($stat1, 0);
                        
        if (is_null($stat2)) {
            //echo "1";
            return true;
        }
        else {
            //echo "0";
            return false;
        }
    }
    
    function fetch_stat($field, $output) {
        $fetch = mysql_query("SELECT $field FROM data_pfields_content WHERE member_id='" . $userno . "'")
        or die(mysql_error());
                
        $$output = mysql_result($fetch, 0);
    }
    
    function zero_stat($field) {
        $fetch = mysql_query("UPDATE data_pfields_content SET $field = '0' WHERE member_id='" . $userno . "'")
        or die(mysql_error());
    }        
?>
dat_dbconn.php:
Code:

<?php

$dbhost = 'xxxxxxxxxxxxx';
$dbuser = 'xxxxxxxxxxxxxxxxxxx';
$dbpass = 'xxxxxxxxxxxxxxxxx'; 
$userno = $_COOKIE["member_id"];

$conn = mysql_connect($dbhost, $dbuser, $dbpass) 
    or die ('Error connecting to mysql');
    
    $dbname = 'xxxxxxxxxxxxxxxxx';
    mysql_select_db($dbname);
?>
All I have to to do get the above code working is cut the code inside the functions and the dat_dbconn.php into the test_newscript.php file and strip them of any function argument calls (substitute them with static values for testing) and it works perfectly, so it has something to do with it all being modular instead of built into the main script.
Since I want to be able to use the functions in other pages that will be created once it all works, it's pretty useful to make things modular, as you might understand.
post #24 of 35
Ah. It looks like field_12 is not defined. Did you mean to use "field_12" instead?
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 #25 of 35
Thread Starter 
I did actually try both with and without quote marks, it didn't make any difference on my end!
post #26 of 35
not the most elegant way to debug things, but you could always write some stuff to a log or to the screen with print_r or var_dump.

If you want to modularize it, don't create the connection in the dbconn.php, put a function in dbconn.php to return a connection to the code that needs it.

also try changing require to include, include shouldn't cause the script to halt on an warning.
(http://www.dynamicdrive.com/forums/s...ad.php?t=25638)

Lastly, try (with phpmyadmin or the mysql console) to execute the query you're trying to use in the code. See if it's actually not returning any results.
Edited by hometoast - 5/6/11 at 1:06pm
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
post #27 of 35
You can probably do it how you are doing it. Just pass $conn as a parameter into your functions that are using the db to get it in scope, and then pass the $conn in as the second param to your mysql_query call and it should work.

Just because it ends up in the same file does not mean it is in scope. You would have to make it global ( which you don't want to do!!! ).

See http://php.net/manual/en/function.mysql-query.php. It is the second optional parameter that would be your $conn.

You should get in the habit of passing the specific connection you want to use. I have never had luck with PHP's "it will use the last opened connection" working reliably.
White TITAN
(16 items)
 
   
CPUMotherboardGraphicsRAM
intel core i7-3770k Asus Z77 Sabertooth EVGA GTX 780 SC ACX Corsair Dominator Platinum 16GB (2x8GB) 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 840 Pro  WD Black Crucial M4 LG Blueray 
CoolingOSMonitorPower
Swiftech H220 (Stock cooler on while RMA :( ) Windows 7 64bit Dell 2311h Ultrasharp x 2 Corsair AX850 
CaseMouseMouse PadAudio
Fractal Design Define R4 White (w/ Window) Logitech G700 Razer Control Edition Sound Blaster ZxR 
CPUMotherboardGraphicsRAM
Intel 980x Asus Rampage iii Extreme EVGA GTX580 SLI Patriot Viper Xtreme 12GB DDR3 2000 (PC3 16000) 
Hard DriveHard DriveHard DriveOptical Drive
120GB Corsair Force Series 3 SSD WD Caviar Black 640GB Crucial M4 128GB SSD LG Black Blu-ray Drive SATA Model UH12LS28 
CoolingCoolingCoolingCooling
XSPC RX360 and RX120 Rads Watercool HeatKiller 3.0 Limited Edition Black ... XSPC Dual Bay Res with Swiftech MCP655 Pump Silver Shining Bitspower Compression Fittings 
CoolingCoolingCoolingCooling
PrimoFlex Pro LRT Black Tubing -1/2in. ID X 3/4in. 3x Gentle Typhoon 120mm Fan 1850RPM (AP-15)  1x Noiseblocker NB-BlackSilentPro PL-2 120mm Ul... Koolance VID-NX580 Full Cover GPU Waterblock x2 
OSMonitorKeyboardPower
Windows 7 Home Premium x64 Dell U2311 Ultrasharp x2 Max Keyboard Nighthawk X8 Semi-Custom - Mechani... Antec High Current Gamer 900W 
CaseMouseMouse PadAudio
Xigmatek Elysium Black Logitech G700 Razer Goliathus Creative Sound Blaster Recon3D Fatal1ty Pro 
  hide details  
Reply
White TITAN
(16 items)
 
   
CPUMotherboardGraphicsRAM
intel core i7-3770k Asus Z77 Sabertooth EVGA GTX 780 SC ACX Corsair Dominator Platinum 16GB (2x8GB) 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 840 Pro  WD Black Crucial M4 LG Blueray 
CoolingOSMonitorPower
Swiftech H220 (Stock cooler on while RMA :( ) Windows 7 64bit Dell 2311h Ultrasharp x 2 Corsair AX850 
CaseMouseMouse PadAudio
Fractal Design Define R4 White (w/ Window) Logitech G700 Razer Control Edition Sound Blaster ZxR 
CPUMotherboardGraphicsRAM
Intel 980x Asus Rampage iii Extreme EVGA GTX580 SLI Patriot Viper Xtreme 12GB DDR3 2000 (PC3 16000) 
Hard DriveHard DriveHard DriveOptical Drive
120GB Corsair Force Series 3 SSD WD Caviar Black 640GB Crucial M4 128GB SSD LG Black Blu-ray Drive SATA Model UH12LS28 
CoolingCoolingCoolingCooling
XSPC RX360 and RX120 Rads Watercool HeatKiller 3.0 Limited Edition Black ... XSPC Dual Bay Res with Swiftech MCP655 Pump Silver Shining Bitspower Compression Fittings 
CoolingCoolingCoolingCooling
PrimoFlex Pro LRT Black Tubing -1/2in. ID X 3/4in. 3x Gentle Typhoon 120mm Fan 1850RPM (AP-15)  1x Noiseblocker NB-BlackSilentPro PL-2 120mm Ul... Koolance VID-NX580 Full Cover GPU Waterblock x2 
OSMonitorKeyboardPower
Windows 7 Home Premium x64 Dell U2311 Ultrasharp x2 Max Keyboard Nighthawk X8 Semi-Custom - Mechani... Antec High Current Gamer 900W 
CaseMouseMouse PadAudio
Xigmatek Elysium Black Logitech G700 Razer Goliathus Creative Sound Blaster Recon3D Fatal1ty Pro 
  hide details  
Reply
post #28 of 35
Thread Starter 
Quote:
Originally Posted by hometoast
If you want to modularize it, don't create the connection in the dbconn.php, put a function in dbconn.php to return a connection to the code that needs it.
Thing is, I didn't want the connection stuff to be in a separate file to begin with; it's just how I left it after I said "ah f*ck it, I'm done with this for now".

Quote:
Originally Posted by hometoast
also try changing require to include, include shouldn't cause the script to halt on an warning.
(http://www.dynamicdrive.com/forums/s...ad.php?t=25638)
The script isn't halting, and require only halts when the file isn't found or otherwise unavailable anyway.

Quote:
Originally Posted by hometoast
Lastly, try (with phpmyadmin or the mysql console) to execute the query you're trying to use in the code. See if it's actually not returning any results.
Both the query and the code to process it are exactly identical to what I have in my old script (everything in one file, no functions, just static processing) and it works perfectly in that one. Put it all in separate functions and modularize it, and somehow it breaks.
It literally is identical in every way except it being arranged into functions in the latter case.

Quote:
Originally Posted by glakr
You can probably do it how you are doing it. Just pass $conn as a parameter into your functions that are using the db to get it in scope, and then pass the $conn in as the second param to your mysql_query call and it should work.
When supplying $conn as the second argument, this happens:
Code:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/deb13109n2/domains/pclv2beta.net/public_html/dat_commoncode.php on line 3


Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/deb13109n2/domains/pclv2beta.net/public_html/dat_commoncode.php on line 5

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/deb13109n2/domains/pclv2beta.net/public_html/dat_commoncode.php on line 18 
post #29 of 35
Did you pass $conn into your function?

You should have
Quote:
function stat_null($field, $conn) //PASS IT IN HERE
{
....
//THEN USE IT HERE
$stat1 = mysql_query("SELECT $field FROM data_pfields_content WHERE member_id='" . $userno . "'", $conn)
....
}
If you are just using $conn inside the function the way you have it setup above, $conn is out of scope.

Then you should be able to call it in your script

Quote:
<html>
<head>
<title>
testing stuff in here!
</title>
</head>
<body>
<?php
require 'dat_dbconn.php';
require 'dat_commoncode.php';

if ($userno == '0') {
echo "USER NOT LOGGED IN!";
}

else {
if(stat_null(field_12, $conn) == '0'){
fetch_stat(field_12, xp);
echo $xp . " xp.";
}
mysql_close($conn);
}
?>
</body>
</html>

Edited by glakr - 5/7/11 at 8:26am
White TITAN
(16 items)
 
   
CPUMotherboardGraphicsRAM
intel core i7-3770k Asus Z77 Sabertooth EVGA GTX 780 SC ACX Corsair Dominator Platinum 16GB (2x8GB) 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 840 Pro  WD Black Crucial M4 LG Blueray 
CoolingOSMonitorPower
Swiftech H220 (Stock cooler on while RMA :( ) Windows 7 64bit Dell 2311h Ultrasharp x 2 Corsair AX850 
CaseMouseMouse PadAudio
Fractal Design Define R4 White (w/ Window) Logitech G700 Razer Control Edition Sound Blaster ZxR 
CPUMotherboardGraphicsRAM
Intel 980x Asus Rampage iii Extreme EVGA GTX580 SLI Patriot Viper Xtreme 12GB DDR3 2000 (PC3 16000) 
Hard DriveHard DriveHard DriveOptical Drive
120GB Corsair Force Series 3 SSD WD Caviar Black 640GB Crucial M4 128GB SSD LG Black Blu-ray Drive SATA Model UH12LS28 
CoolingCoolingCoolingCooling
XSPC RX360 and RX120 Rads Watercool HeatKiller 3.0 Limited Edition Black ... XSPC Dual Bay Res with Swiftech MCP655 Pump Silver Shining Bitspower Compression Fittings 
CoolingCoolingCoolingCooling
PrimoFlex Pro LRT Black Tubing -1/2in. ID X 3/4in. 3x Gentle Typhoon 120mm Fan 1850RPM (AP-15)  1x Noiseblocker NB-BlackSilentPro PL-2 120mm Ul... Koolance VID-NX580 Full Cover GPU Waterblock x2 
OSMonitorKeyboardPower
Windows 7 Home Premium x64 Dell U2311 Ultrasharp x2 Max Keyboard Nighthawk X8 Semi-Custom - Mechani... Antec High Current Gamer 900W 
CaseMouseMouse PadAudio
Xigmatek Elysium Black Logitech G700 Razer Goliathus Creative Sound Blaster Recon3D Fatal1ty Pro 
  hide details  
Reply
White TITAN
(16 items)
 
   
CPUMotherboardGraphicsRAM
intel core i7-3770k Asus Z77 Sabertooth EVGA GTX 780 SC ACX Corsair Dominator Platinum 16GB (2x8GB) 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 840 Pro  WD Black Crucial M4 LG Blueray 
CoolingOSMonitorPower
Swiftech H220 (Stock cooler on while RMA :( ) Windows 7 64bit Dell 2311h Ultrasharp x 2 Corsair AX850 
CaseMouseMouse PadAudio
Fractal Design Define R4 White (w/ Window) Logitech G700 Razer Control Edition Sound Blaster ZxR 
CPUMotherboardGraphicsRAM
Intel 980x Asus Rampage iii Extreme EVGA GTX580 SLI Patriot Viper Xtreme 12GB DDR3 2000 (PC3 16000) 
Hard DriveHard DriveHard DriveOptical Drive
120GB Corsair Force Series 3 SSD WD Caviar Black 640GB Crucial M4 128GB SSD LG Black Blu-ray Drive SATA Model UH12LS28 
CoolingCoolingCoolingCooling
XSPC RX360 and RX120 Rads Watercool HeatKiller 3.0 Limited Edition Black ... XSPC Dual Bay Res with Swiftech MCP655 Pump Silver Shining Bitspower Compression Fittings 
CoolingCoolingCoolingCooling
PrimoFlex Pro LRT Black Tubing -1/2in. ID X 3/4in. 3x Gentle Typhoon 120mm Fan 1850RPM (AP-15)  1x Noiseblocker NB-BlackSilentPro PL-2 120mm Ul... Koolance VID-NX580 Full Cover GPU Waterblock x2 
OSMonitorKeyboardPower
Windows 7 Home Premium x64 Dell U2311 Ultrasharp x2 Max Keyboard Nighthawk X8 Semi-Custom - Mechani... Antec High Current Gamer 900W 
CaseMouseMouse PadAudio
Xigmatek Elysium Black Logitech G700 Razer Goliathus Creative Sound Blaster Recon3D Fatal1ty Pro 
  hide details  
Reply
post #30 of 35
Thread Starter 
Ah, missed that one indeed. I still get the feeling it's not even executing the query though or something, because now I'm back to this:
Code:

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 4 in /home/deb13109n2/domains/pclv2beta.net/public_html/dat_commoncode.php on line 16

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 5 in /home/deb13109n2/domains/pclv2beta.net/public_html/dat_commoncode.php on line 32 
And again, the query and the processing of its results works fine if I put it all in one script, but once that is put on an external script it's all buggered.

Also, I rejoined the connection data with the common code which eliminates dat_dbconn.php, so dat_commoncode.php is now the following:

Code:

<?php
    $dbhost = 'localhost';
    $dbuser = 'deb13109n2_kami';
    $dbpass = 'pclno1bankami'; 
    $userno = $_COOKIE["member_id"];

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) 
    or die ('Error connecting to mysql');
    
    $dbname = 'deb13109n2_pclv2';
    mysql_select_db($dbname);
    
    function stat_null($field, $conn) {
        $stat1 = mysql_query("SELECT $field FROM data_pfields_content WHERE member_id='" . $userno . "'", $conn)
        or print(mysql_error());
        $stat2 = mysql_result($stat1, 0);
                        
        if (is_null($stat2)) {
            //echo "1";
            return true;
        }
        else {
            //echo "0";
            return false;
        }
    }
    
    function fetch_stat($field, $output, $conn) {
        $fetch = mysql_query("SELECT $field FROM data_pfields_content WHERE member_id='" . $userno . "'", $conn)
        or die(mysql_error());
                
        $$output = mysql_result($fetch, 0);
    }
    
    function zero_stat($field, $conn) {
        $fetch = mysql_query("UPDATE data_pfields_content SET $field = '0' WHERE member_id='" . $userno . "'", $conn)
        or die(mysql_error());
    }        
?>

Edited by RevZ - 5/7/11 at 10:41am
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