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.

    One way of debugging code I find very useful is to output your varibles at various stages of the code. For instance if was debugging this i would first make sure that the correct values were actualy getting pulled out of the DB.

    I would then use a print_r() to show the values in $dataset[] to make sure we have the correct values in the first place.

    By printing your varibles at certain stages it makes it so much easy to find a bug.

    Hope this helps

    Mark.

      As marky2coats sez, debugging is a PITA.

      There's a "debugging hints" thread that might be worth a read.

      Some more strategies:

      echo $vars to the screen, check against your anticipated values
      echo sql statements to the screen, run them in the SQL monitor
      and check for values
      put an echo statement in each loop that sez "I'm looping here now, and such-n-so variable is $value" --- check progress as you go
      As Mark said, use print_r($function_results) to see the contents of the array after you set it to the function's value.

      Other thoughts on your script...

      Isset() may not be necessary, but I could be grasping at a straw there. Probably calling if ($_SESSION['var']) would simply give the same results.

      No reason to declare "global $function_results." It's being returned by the function anyway.

      You are calling "$function_results=array();" twice, once in the function and once prior to calling it. Not necessary.

      Check the value you're printing to the page without any if, ands, buts, or money_formats. Could be that simple, I guess, not having run the code myself.

      The code looks pretty, it must be something we're overlooking.

        Thanks! I completely forgot about printing the variables at the different stages. I have not found out that the function isn't even getting called (I think). None of the printing functions I have used are getting printed (print_r, echo).

        Hmmm... I will keep hammering it out. Thank you for you replies...

          Write a Reply...