Page 1 of 1

PHP out of memory

Posted: Sat Apr 26, 2014 10:43 am
by nite4000
I am having a issue with a out of memory error when I run a file I have had the host increase the memory with no luck I have done research and read its related to too much being held in memory however I do not know how to get this corrected

I am providing all my code. I have traced it to the code at the bottom after the functions

Code: Select all

<?php
include 'inc/database.class.php';
	
	function bind_result_array($stmt) {
    $meta = $stmt->result_metadata();
    
		$result = array();

    while ($field = $meta->fetch_field()) {
      $result[$field->name] = NULL;
      $params[] = &$result[$field->name];
    }

    call_user_func_array(array($stmt, 'bind_result'), $params);
    return $result;
	}
	function getCopy($row) {
		return array_map(create_function('$a', 'return $a;'), $row);
	}
		
	function get_active_accounts() {

			global $conn;

			$accounts = array();

			$sql = 'SELECT * FROM accounts WHERE acct_status = "Active"';
			$qry = $conn->query($sql);
			if ($qry === false) {
				die('Error: ' . $conn->error . '<br/><i>' . $sql . '</i>');
			}
			while($row = $qry->fetch_assoc())
				$accounts[] = $row;

			$qry->free();

			return $accounts;
		}
	
	function get_account_plans($account) {

			global $conn;

			$plans = array();

			$sql = 'SELECT 
								inv.id, 
								inv.username,
								inv.user_id,
								inv.plan_id,
								inv.plan_name,
								inv.total_invest,
								inv.rate, 
								inv.`date`,
								inv.compound_rate,
								plans.parent, 
								packages.days
							FROM 
								investments AS inv
							INNER JOIN 
								plans ON inv.plan_id = plans.id
							INNER JOIN 
								packages ON plans.parent = packages.id
							WHERE 
								inv.user_id = ?
							AND inv.end_date > NOW()';
			$stmt = $conn->prepare($sql);
			if ($stmt === false) {
				die('Error: ' . $conn->error . '<br/><i>' . $sql . '</i>');
			}
			$stmt->bind_param('i', $account);
			$stmt->execute();

			$row = bind_result_array($stmt);

			while($stmt->fetch())
				$plans[] = getCopy($row);

			$stmt->close();

			return $plans;
		}

	function need_to_pay($last_paid, $pay_frequency) {
	
			$last_payment_date = strtotime($last_paid);
			$days_since_last_payment = floor((time() - $last_payment_date) / 86400);
			if ($days_since_last_payment >= $pay_frequency)
				return TRUE;
			else
				return FALSE;
		}	
		
	function credit_account($acc_id, $inv_id, $inv_amount, $int_rate, $comp_rate) {

			global $conn;

			$sql = 'SELECT * FROM package_settings WHERE id = 1';
			$qry = $conn->query($sql);
			if ($qry === false) {
				die('Error: ' . $conn->error . '<br/><i>' . $sql . '</i>');
			}

			$f_setting45 = $qry->fetch_assoc();

			$qry->free();

			$interest = $inv_amount * $int_rate / 100;

			if($f_setting45['allow_compounding']) {

				$comp_amount = $interest * $comp_rate / 100;
				$int_left = $interest - $comp_amount;
				echo 'Amount Invested: ' . $inv_amount;
				echo '<br/>';
				echo 'Interest Rate: ' . $int_rate;
				echo '<br/>';
				echo 'Amount of interest: ' . $interest;
				echo '<br/>';
				echo 'Compound Rate: ' . $comp_rate;
				echo '<br/>';
				echo 'Amount that will be added to invested amount: ' . $comp_amount;
				echo '<br/>';
				echo 'This is the left over: ' . $int_left;
				echo '<br/>';

				if($comp_rate == 100) {
					$sql = 'UPDATE investments SET total_invest = total_invest + ' . $interest . ' WHERE id = ?';
					$qry = $conn->prepare($sql);
					if (!$qry) {
						die('Error: ' . $conn->error . '<br/><i>' . $sql . '</i>');
					}
					$qry->bind_param('i', $inv_id);
					$qry->execute();
					$qry->close();
				} else {
					$sql = 'UPDATE investments SET invested = invested + ' . $comp_amount . ' WHERE id = ?';
					$qry = $conn->prepare($sql);
					if (!$qry) {
						die('Error: ' . $conn->error . '<br/><i>' . $sql . '</i>');
					}
					$qry->bind_param('i', $inv_id);
					$qry->execute();
					$qry->close();
				}

				$sql = 'UPDATE accounts SET balance = balance + ' . $int_left . ', earned = earned + ' . $int_left . ' WHERE user_id = ?';
				$qry = $conn->prepare($sql);
				if (!$qry) {
					die('Error: ' . $conn->error . '<br/><i>' . $sql . '</i>');
				}
				$qry->bind_param('i', $acc_id);
				$qry->execute();
				$qry->close();

				return $int_left;
			} else {

				$interest = $inv_amount * $int_rate / 100;

				$sql = 'UPDATE accounts SET balance = balance + ' . $interest . ', earned = earned + ' . $interest . ' WHERE user_id = ?';
				$qry = $conn->prepare($sql);
				if (!$qry) {
					die('Error: ' . $conn->error . '<br/><i>' . $sql . '</i>');
				}
				$qry->bind_param('i', $acc_id);
				$qry->execute();
				$qry->close();

				return $interest + $inv_amount;
			}
		}		
		
	function update_pay_date($investment) {
			
			global $conn;
			
			$sql = 'UPDATE investments SET `date` = NOW() WHERE id = ?';
			$stmt = $conn->prepare($sql);
			if ($stmt === false) {
				die('Error: ' . $conn->error . '<br/><i>' . $sql . '</i>');
			}
			$stmt->bind_param('i', $investment);
			$stmt->execute();
			$stmt->close();
		}

		
	$sql = "SELECT * FROM package_settings WHERE id = 1";
	$qry = $conn->query($sql);
	if ($qry === false) {
		die('Error: ' . $conn->error . '<br/><i>' . $sql . '</i>');
	}
	$pack_settings = $qry->fetch_assoc();
	$qry->free();
	
	$is_need_to_pay = false;
	
	$debug = false;

	if($pack_settings['week_pay_only'] == 1) {
		$day_of_week = date("l");//get todays weekday
		
		if(($day_of_week == 'Saturday') || ($day_of_week == 'Sunday'))
			echo 'Today is a weekend. Come back monday. You wont get paid today';
    else 
      $is_need_to_pay = true;
    
	} else 
    $is_need_to_pay = true;
	

	
	if ($is_need_to_pay) {
	
		
		$to_pay = array();
		$accounts = get_active_accounts();
		
		if ($debug === true)
			echo "Active accounts:<br />"; 
		
		foreach ($accounts as $account) {
			if ($debug === true) 
				echo $account['id'] . "<br />"; 

			$plan = get_account_plans($account['user_id']);

			if (!empty($plan))
				$to_pay[] = $plan;
		}
		
		if ($debug === true)
			echo '<hr />Accounts to be paid:<br /><br />';
		foreach ($to_pay as $payee) {
			foreach ($payee as $entry) {
				if (need_to_pay($entry['date'], $entry['days']) === true) {
					if ($debug === true)
						echo 'Account: {' . $entry['user_id'] . '}<br /><br />';
						
					$earn = credit_account($entry['user_id'], $entry['id'], $entry['total_invest'], $entry['rate'], $entry['compound_rate']);	
					update_pay_date($entry['id']);
					
					$sql = 'INSERT INTO earnings (id, user_id, amount, plan, date) VALUES (null, IFNULL(?, user_id), IFNULL(?, amount), IFNULL(?, plan), NOW())';
					$stmt = $conn->prepare($sql);
					if (!$stmt) {
						die('Error: ' . $conn->error . '<br/><i>' . $sql . '</i>');
					}
					$stmt->bind_param('sss', $entry['user_id'], $earn, $entry['plan_name']);
					$stmt->execute();
					
					$stmt->close();
					
				} // if (need_to_pay...
			} // foreach ($payee...
		}	// foreach ($to_pay...
		
		
		$today = date("Y-m-d", time());
		echo $today;	
		
		
	} //if ($is_need_to_pay)
	
?>
The guy who helped write this is lost at what it could be

This is written with mysqli


The error I get is
Fatal error: Out of memory (allocated 524288) (tried to allocate 4294967296 bytes) in /home/awesomes/public_html/ultra/index_cron.php on line 16


the code on line 16 is
call_user_func_array(array($stmt, 'bind_result'), $params);


any help would be great