Jump to content

[SOLVED] Sometimes variable is there and sometimes not!


mike12255

Recommended Posts

I got a peice of code which in basic is just a function with a query in it. Sometimes the variable $amount is non existant though because the the user does not have the correct session. This is a simple placement error of my if statment im sure, i just cant tell were i should move the else statment, or the if statment. Anyone got ideas?

 

<?php
function getqty(){

$cartContent = array();

$sid = session_id();

$pid = $_GET['p'];
$pid = mysql_real_escape_string($pid)
$sql = "SELECT * FROM tbl_cart WHERE ct_session_id = '$sid' AND pd_id = '$pid'";

if ($result = mysql_query($sql)){

while($row = mysql_fetch_array($result)){
	$amount = $row['ct_qty'];
}
}else{
	$amount = "0";
}
return $amount;
}?>

Not sure if this make sense but.

 

Say the session is empty or wrong. Then your:

if ($result = mysql_query($sql))

 

Is going to return nothing. And so your

return $amount;

will return empty?

 

Maybe put in a if ct_session_id == EMPTY  or NULL then set a value for $amount.

 

if session != empty do SQL else return $amount = 0;

 

Not sure if that is right or makes sense?

 

I tried the to things below, just getting the same error.

 

Notice: Undefined variable: amount in /home/schoolw1/public_html/nifty/library/cart-functions.php on line 98

You currently have of Medium Impression Kit in your cart,

 

** Line 98 is the return amount line

 

<?php
function getqty(){
   
   $cartContent = array();

   $sid = session_id();
   
   $pid = $_GET['p'];
   $pid = mysql_real_escape_string($pid)
   $sql = "SELECT * FROM tbl_cart WHERE ct_session_id = '$sid' AND pd_id = '$pid'";
   

if(!empty($sid)){
$result = mysql_query($sql);
   
   while($row = mysql_fetch_array($result)){
      $amount = $row['ct_qty'];
   }else{
$amount = "0";
}
   return $amount;
}?>

 

<?php
function getqty(){
   
   $cartContent = array();

   $sid = session_id();
   
   $pid = $_GET['p'];
   $pid = mysql_real_escape_string($pid)
   $sql = "SELECT * FROM tbl_cart WHERE ct_session_id = '$sid' AND pd_id = '$pid'";
   

$result = mysql_query($sql) or die (mysql_error());
   
   while($row = mysql_fetch_array($result)){
      $amount = $row['ct_qty'];
   return $amount;
}?>

I just preset $amount and it seems if the amount of rows is non existant it dosnt overwrite the variable:

 

$amount = 0;


$result = mysql_query($sql) or die (mysql_error());

while($row = mysql_fetch_array($result)){
	$amount = $row['ct_qty'];
}

return $amount;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.