I am (obviously) new to PHP. As I create this new site I am working on, I am in the process of writing my first database-driven page.
I have read the sticky bulletin messages, and they unfortunately were not very helpful to me. I am sorry if this new thread disrupts or otherwise offends anyone, but I am getting frustrated beyond reason in getting this code to work right. In attempts to not ask the "same ol' question" again, I have also been searching the web for the answer(s) that may make this code work.
What I am trying to do is...
I query the database, get the value(s) I need, then return them as an array for the rest of the page. See the code:
<?php
/*
Functions like this I normally add-on to the bottom of the page I am working on.
*/
function AssignValues(){
global $function_results;
$function_results=array();
if (isset($_SESSION["package"])) {
$dbh=mysql_pconnect("localhost","username","password") or die('I cannot connect to the database because: ' . mysql_error());
mysql_select_db("dbname");
# Initial query to get the package price
$sql=mysql_query("SELECT * FROM ssd_pricelist WHERE pl_sku = '".$_SESSION["package"].$_SESSION["payment_plan"]."'") or die("Invalid query 01: " . mysql_error());
$dataset=mysql_fetch_array($sql,MYSQL_ASSOC);
$PackagePrice=$dataset["pl_price"];
$AssocSKU=$dataset["pl_assoc_sku"];
$sql=mysql_free_result($sql);
# Requery to get the fees
$sql=mysql_query("SELECT * FROM ssd_pricelist WHERE pl_sku = '".$AssocSKU."'") or die("Invalid query 02: " . mysql_error());
$dataset=mysql_fetch_array($sql,MYSQL_ASSOC);
$PackageSetUpFee=$dataset["pl_price"];
$sql=mysql_free_result($sql);
# Create the array which will hold the values I am needing to use
$AddOnsPrice=array("bandwidth"=>0.00,"web_space"=>0.00,"emails"=>0.00,"list"=>0.00);
# Requery and assign the add-ons to the ASSOC array...
if (isset($_SESSION["bandwidth"])) {
$sql=mysql_query("SELECT * FROM ssd_pricelist WHERE pl_sku = '".$_SESSION["bandwidth"]."'") or die("Invalid query 03: " . mysql_error());
$dataset=mysql_fetch_array($sql,MYSQL_ASSOC);
$AddOnsPrice["bandwidth"]=$dataset["pl_price"];
$sql=mysql_free_result($sql);
}
# I have removed a few other queries which are exactly the same as the one above ^, except with different query filters to populate the rest of the ASSOC array, $AddOnsPrice().
$AddOnsTotal=($AddOnsPrice["bandwidth"]+$AddOnsPrice["web_space"]+$AddOnsPrice["emails"]+$AddOnsPrice["list"]);
#mysql_close($dbh);
}else{
$PackagePrice=0.00;
$PackageSetUpFee=0.00;
$AddOnsPrice=0.00;
$AddOnsTotal=0.00;
}
$function_results["packageprice"]=$PackagePrice;
$function_results["packagesetupfee"]=$PackageSetUpFee;
$function_results["addonsprice"]=$AddOnsPrice;
$function_results["addonstotal"]=$AddOnsTotal;
return $function_results;
} # End Function
?>
Now, I retrieve/assign the return values like this:
<?php
/*
This is near the top of the page
*/
# Function Variable
global $function_results;
$function_results=array();
# Call the Function to populate the array with values
$function_results=AssignValues();
?>
I am getting no errors, but the values which I am intending to have retrieved are all coming out zeroes. This is either because they are null, or getting assigned as zeroes (I think).
Here is what I have tried thusfar:
1. I have taken the IF statement out of the equation to make sure that the code is actually going through the processes of accessing the database.
2. I have also taken out the ELSE portion of the IF statement in order to check that as well.
3. Originally, I was simply calling the function. Now I am calling the function while assigning (supposedly) a variable value(s).
4. I have also been tirelessly going through the code over and over again, looking for any syntax errors I may be overlooking.
I am probably missing something simple. Why is my array not getting the values I am intending them to be? They are supposed to be dollar values.
The values are getting echoed into the page like this:
<?php
if(substr($_SESSION["package"],0,2)=="wd"){echo money_format('%i',$function_results["packagesetupfee"]);}
# Output is: USD 0.00
?>
I am hoping that someone could shed a little light on what I may be missing here. I am guessing that I am probably trying to do something in a way the code wasn't intended to let me do it. It would be my luck, and it wouldn't be the first time.
EDIT: By the way, in case it matters, I am running on a remotely hosted web server which is running Red Hat Linux, Apache ver 1.3.28 (Unix), PHP ver 4.3.2, and MySQL ver 3.23.56.