George Botley Posted June 1, 2011 Share Posted June 1, 2011 Hello All, I have used a function to collect a users information from the database. This is then returned as an array. Using this returned array, I wish to pass it into another function. Here is the code: The array is created in the collect() function and needs to be passed to the ipn_data function /* This script is responsible for handling all subscription payments. It will collect all information, insert data into the relevant databases and then forward the user to PayPal with the relevant transaction ID. On return, the IPN listener will deal with all account changes whilst the user is held in a queue. */ /* ------------------------------------- */ /* DATABASE CONFIGURATION */ /* ------------------------------------- */ include_once "../config/config.php"; /* ------------------------------------- */ /* COLLECT SESSION INFORMATION */ /* ------------------------------------- */ session_start(); $id = "100000002"; // ID of logged in user $next_year = date("Y-m-d", strtotime("+365 day", time())); //Date next year /* ------------------------------------- */ /* SET VARIABLES */ /* ------------------------------------- */ $reason = $_GET["reason"]; /* ----------------------------------------- */ /* COLLECT USERS INFORMATION FROM DATABASE */ /* ----------------------------------------- */ function collect($id) { //Select the subscriber from the database $query = "SELECT * FROM subscribers WHERE id='$id'"; $query = mysql_query($query); //Collect the subscribers first name, last name and email address while($row=mysql_fetch_array($query)) { $fname = $row["fname"]; $lname = $row["lname"]; $email = $row["email"]; } //Collate subscribers information into an array return array('fname' => "$fname", 'lname' => "$lname", 'email' => "$email"); } /* ----------------------------------------- */ /* CREATE TIMESTAMP OF CURRENT TIME */ /* ----------------------------------------- */ function mktimestamp() { //Set date variables $day = date("d"); $month = date("m"); $year = date("Y"); //Make timestamp return mktime(0, 0, 0, $month, $day, $year); } /* ----------------------------------------- */ /* INSERT USERS DATA INTO ipn_data */ /* ----------------------------------------- */ //Create random identifier $ident = rand("1", "10000"); function ipn_data($info, $reason, $timestamp) { //WANT THE ARRAY TO BE PASSED INTO HERE!! //I TRIED PASSING THE $info VARIABLE INTO THE FUNCTION, WHICH IS WHAT I ASSIGNED TO THE COLLECT FUNCTION LATER IN THE SCRIPT } /* ----------------------------------------- */ /* Get ID of inserted ipn_data row */ /* ----------------------------------------- */ function get_id($ident, $timestamp) { //Setup Query to find ID $query = "SELECT * FROM subscribers WHERE timestamp='$timestamp' AND identifier='$ident'"; //Run Query $query = mysql_query($query) or mysql_error(); //Using query find ID while($row=mysql_fetch_array($query)) { $ltj_txn_id = $row["id"]; } return $ltj_txn_id or mysql_error(); } /* ------------------------------------- */ /* SWITCH REASON */ /* ------------------------------------- */ switch($reason) { case "subscribe": break; case "renew": //Collect array from user information array function $info = collect($id); //Get timestamp from our mktimestamp function $timestamp = mktimestamp(); //Insert users data into ipn_data table $run_ipn = ipn_data($reason, $timestamp, $ident); //Collect ID of record just inserted $get_id = get_id($ident, $timestamp); echo "$run_ipn"; break; case "upgrade": break; default: break; } Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/238087-pass-an-array-as-function-element/ Share on other sites More sharing options...
trq Posted June 1, 2011 Share Posted June 1, 2011 I didn't look at your code but passing an array is no different to passing any other variable. $a = array(); somefunction($a); Quote Link to comment https://forums.phpfreaks.com/topic/238087-pass-an-array-as-function-element/#findComment-1223442 Share on other sites More sharing options...
gristoi Posted June 1, 2011 Share Posted June 1, 2011 $userDetails = collect($id); ipn_data($userDetails , $reason, $timestamp); Quote Link to comment https://forums.phpfreaks.com/topic/238087-pass-an-array-as-function-element/#findComment-1223444 Share on other sites More sharing options...
George Botley Posted June 1, 2011 Author Share Posted June 1, 2011 Okay, well I have been trying exactly what you guys have said and I get no returns from the array within the function and therefore see a mysql error as the values of the array are empty. Yet when running a print_r() on the array outside of the function I get all values as expected. Quote Link to comment https://forums.phpfreaks.com/topic/238087-pass-an-array-as-function-element/#findComment-1223453 Share on other sites More sharing options...
KevinM1 Posted June 1, 2011 Share Posted June 1, 2011 Can you show the error? Can you show the new code you use to invoke these functions with the array being passed into the function? Also, you don't need a while-loop in your collect() function. You're only grabbing one row of db data, so there's no reason to have a loop there. You also don't need to put the values ($fname, $lname, etc.) in quotes as they're already strings. Quote Link to comment https://forums.phpfreaks.com/topic/238087-pass-an-array-as-function-element/#findComment-1223455 Share on other sites More sharing options...
George Botley Posted June 1, 2011 Author Share Posted June 1, 2011 <?php /* This script is responsible for handling all subscription payments. It will collect all information, insert data into the relevant databases and then forward the user to PayPal with the relevant transaction ID. On return, the IPN listener will deal with all account changes whilst the user is held in a queue. */ /* ------------------------------------- */ /* DATABASE CONFIGURATION */ /* ------------------------------------- */ include_once "../config/config.php"; /* ------------------------------------- */ /* COLLECT SESSION INFORMATION */ /* ------------------------------------- */ session_start(); $id = $_SESSION["loggedin_id"]; $id = "100000002"; // ID of logged in user $next_year = date("Y-m-d", strtotime("+365 day", time())); //Date next year /* ------------------------------------- */ /* SET VARIABLES */ /* ------------------------------------- */ $reason = $_GET["reason"]; /* ----------------------------------------- */ /* COLLECT USERS INFORMATION FROM DATABASE */ /* ----------------------------------------- */ function collect($id) { //Select the subscriber from the database $query = "SELECT * FROM subscribers WHERE id='$id'"; $query = mysql_query($query); //Collect the subscribers first name, last name and email address while($row=mysql_fetch_array($query)) { $fname = $row["fname"]; $lname = $row["lname"]; $email = $row["email"]; } //Collate subscribers information into an array return array('fname' => $fname, 'lname' => $lname, 'email' => $email); } /* ----------------------------------------- */ /* CREATE TIMESTAMP OF CURRENT TIME */ /* ----------------------------------------- */ function mktimestamp() { //Set date variables $day = date("d"); $month = date("m"); $year = date("Y"); //Make timestamp return mktime(0, 0, 0, $month, $day, $year); } /* ----------------------------------------- */ /* INSERT USERS DATA INTO ipn_data */ /* ----------------------------------------- */ //Create random identifier $ident = rand("1", "10000"); function ipn_data($info, $reason, $timestamp, $ident) { //Prepare Query $query = "INSERT INTO ipn_data (fname, lname, email, payment_reason, timestamp, identifier) VALUES ('{$info['fname']}', '{$info['lname']}', '{$info['email']}, '$reason', '$timestamp', '$ident')"; //Run Query $query = mysql_query($query) or die(mysql_error()); } /* ----------------------------------------- */ /* Get ID of inserted ipn_data row */ /* ----------------------------------------- */ function get_id($ident, $timestamp) { } /* ------------------------------------- */ /* SWITCH REASON */ /* ------------------------------------- */ switch($reason) { case "subscribe": break; case "renew": //Collect array from user information array function $info = collect($id); //Get timestamp from our mktimestamp function $timestamp = mktimestamp(); //Insert users data into ipn_data table $run_ipn = ipn_data($info, $reason, $timestamp, $ident); //Collect ID of record just inserted $get_id = get_id($ident, $timestamp); break; case "upgrade": break; default: break; } ?> Here is the entire code... It seems the array now passes, odd... but I now get this: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'renew', '1306882800', '1120')' at line 5 This is within the ipn_data function... Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/238087-pass-an-array-as-function-element/#findComment-1223464 Share on other sites More sharing options...
AbraCadaver Posted June 1, 2011 Share Posted June 1, 2011 You're missing the closing quote here: '{$info['email']} Also, check this for the insert: http://www.phpfreaks.com/forums/index.php?topic=331444.msg1559950#msg1559950 And assuming collect() only retrieves one record, do this: function collect($id) { //Select the subscriber from the database, only 1 $query = "SELECT * FROM subscribers WHERE id='$id' LIMIT 1"; $result = mysql_query($query); //Return the subscribers first name, last name and email address, already in an array return $row = mysql_fetch_array($result, MYSQL_ASSOC); } Quote Link to comment https://forums.phpfreaks.com/topic/238087-pass-an-array-as-function-element/#findComment-1223470 Share on other sites More sharing options...
George Botley Posted June 1, 2011 Author Share Posted June 1, 2011 Thanks Guys, Almost there, just one more thing.. In my get_id() function, I wish to get the auto_increment ID of the query that happened within the ipn_data() query... Here is the code I used: function get_id($ident, $timestamp) { //Prepare Query $query = "SELECT FROM ipn_data WHERE ident='$ident' AND timestamp='$timestamp'"; //Run Query $query = mysql_query($query); //Collect ID $row = mysql_fetch_array($query, MYSQL_ASSOC); $id = $row["id"]; return $id; } When then running the script I get this: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in/home/letterst/public_html/sandbox/includes/functions/payment.phpon line97 Line 97 being this line: $row = mysql_fetch_array($query, MYSQL_ASSOC); Quote Link to comment https://forums.phpfreaks.com/topic/238087-pass-an-array-as-function-element/#findComment-1223507 Share on other sites More sharing options...
AbraCadaver Posted June 1, 2011 Share Posted June 1, 2011 $query = mysql_query($query) or die(mysql_error()); It will lead you to: $query = "SELECT WHAT FROM ipn_data WHERE ident='$ident' AND timestamp='$timestamp'"; Quote Link to comment https://forums.phpfreaks.com/topic/238087-pass-an-array-as-function-element/#findComment-1223528 Share on other sites More sharing options...
George Botley Posted June 1, 2011 Author Share Posted June 1, 2011 Thanks Guys. Quote Link to comment https://forums.phpfreaks.com/topic/238087-pass-an-array-as-function-element/#findComment-1223533 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.