functions and database connections

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
press711
Forum Newbie
Posts: 5
Joined: Wed Jan 15, 2003 1:50 am

functions and database connections

Post by press711 »

Hii,

I have a include file in which I have a few functions. For one of these function I need to get certain configuration settings stored as fields in a mysql database. However my database connection code inside the function seems to work as long as it is outside the function. The moment I put the code into the function it starts revolting with : "Warning: Supplied argument is not a valid MySQL-Link resource in /var/www/kunal/WIMS/includes/billCalc.php on line 34"

My code for the function (that is line 33 onwards ) is :

function calculateAmt($myCurrItemQua, $myCurrItemRate, $myEvent){

mysql_select_db($database_wims, $wims);
$query_payConfig = "SELECT * FROM price_config WHERE STATUS = '1' AND PRI_CONFIGEVENT ='".GetSQLValueString1($myEvent,"text")."'";
$RecpayConfig = mysql_query($query_payConfig, $wims) or die(mysql_error());
$row_RecpayConfig = mysql_fetch_assoc($RecpayConfig);
$totalRows_RecpayConfig = mysql_num_rows($RecpayConfig);
switch($row_RecpayConfig["PRI_CONFIGUSAGE"]){
case "2":
//has to be billed at the that moment
//will have to proceed with payment gateway
//if(txtTotItems
return $myCurrItemQua.", ".$myCurrItemRate."<br/>";
//return "hello";

break;
case "0":
//has to be billed at the end of month
//just find out the costs and add it without a corresponding invoice id to the table.
//echo "hello2";
return "hello2";
break;
}
}

i also have at the top of the code :
require_once('Connections/wims.php');

this connection file looks like
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_wims = "localhost";
$database_wims = "databasename";
$username_wims = "username";
$password_wims = "password";
$wims = mysql_pconnect($hostname_wims, $username_wims, $password_wims) or die(mysql_error());
?>

Thanks for your help in advance.

Regards,
Kunal 8O
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

Hi,

If you want to access variables which are out side the function, you need to define/access them as "global", like:

Code: Select all

<?php

$i = 5;
$name = 'some name';


function abc($x)
{
    global $i, $name;

    echo $name . '<BR>';
    echo 'Result: ' . $i + $x;
}

?>
Chk also:

:arrow: http://www.php.net/manual/en/language.v ... .scope.php

Regards,
User avatar
press711
Forum Newbie
Posts: 5
Joined: Wed Jan 15, 2003 1:50 am

Thanks for the help

Post by press711 »

Well you were right in saying that the function is unable to access thevariables but glbal didnt seem to do the trick.

I changed the way it was calling the file by adding it as a parameter and that worked.
Not the thing which I wanted to do, but I am in a bit of hurry and dont seem to have many options.

Do let me know if you find a more "noble" way fo doing it. Email :press711@yahoo.com.au

mysql_select_db($database_wims, $wims);
echo calculateAmt($txtItemQua[$count], $txtItemRate[$count], $txtEvntType, $wims);

Thanks,
Kunal
Post Reply