vanquish12 Posted February 26, 2015 Share Posted February 26, 2015 Is it possible to call a function from another function and update a mysql database using the 2nd function? So if I have 1 function which selects data from a database and puts it into an array, I then need the 2nd function to do some stuff and insert the results to another table. Can the insert statement be in the 2nd function or does it have to be a seperate statement? Link to comment https://forums.phpfreaks.com/topic/294914-call-a-function-from-a-function-and-update-mysql-db/ Share on other sites More sharing options...
Ch0cu3r Posted February 26, 2015 Share Posted February 26, 2015 Yes you can call a function within a function. Without seeing your code cant give you a specific answer. Link to comment https://forums.phpfreaks.com/topic/294914-call-a-function-from-a-function-and-update-mysql-db/#findComment-1506800 Share on other sites More sharing options...
vanquish12 Posted February 26, 2015 Author Share Posted February 26, 2015 function function1(){ $sql = "SELECT a, b, c, d FROM someTable"; $result = mysqli_query($conn, $sql) or die(mysql_error()); if(mysqli_num_rows($result) > 0) { //output data while($row = mysqli_fetch_array($result)) { $row = array_map("function2", $row['a'], $row['b'], $row['c']); } } mysqli_close($conn); } function function2($a, $b, $c) { $table = "some table"; $fsock = fsockopen($a, $b, $c); if (!$fsock) { $res= 0; return $res; } else { $res= 1; return $res; } } Link to comment https://forums.phpfreaks.com/topic/294914-call-a-function-from-a-function-and-update-mysql-db/#findComment-1506815 Share on other sites More sharing options...
cyberRobot Posted February 26, 2015 Share Posted February 26, 2015 Edit: array_map would process the values one index at a time. Have you tried calling the function directly? $row = function2($row['a'], $row['b'], $row['c']); The second argument for array_map needs to be an array. More information can be found here: http://php.net/manual/en/function.array-map.php You could try passing $row as is: $row = array_map("function2", $row); Of course, you would need to modify function2 to accept a single argument. Link to comment https://forums.phpfreaks.com/topic/294914-call-a-function-from-a-function-and-update-mysql-db/#findComment-1506816 Share on other sites More sharing options...
vanquish12 Posted February 26, 2015 Author Share Posted February 26, 2015 thanks, can I also include an mysql insert statement in the 2nd function so it would look like: function function2($a, $b, $c) { $table = "some table"; $fsock = fsockopen($a, $b, $c); if (!$fsock) { $res= 0; return $res; } else { $res= 1; return $res; } mysqli_query($conn, "INSERT INTO $table (a, b) VALUES ('" . $a . "', '" . $b. "') or die(mysqli_error($conn)); } Link to comment https://forums.phpfreaks.com/topic/294914-call-a-function-from-a-function-and-update-mysql-db/#findComment-1506818 Share on other sites More sharing options...
cyberRobot Posted February 26, 2015 Share Posted February 26, 2015 thanks, can I also include an mysql insert statement in the 2nd function... You could give it a try, but you'll probably need to pass the connection link ($conn) to the function for it to work. Link to comment https://forums.phpfreaks.com/topic/294914-call-a-function-from-a-function-and-update-mysql-db/#findComment-1506819 Share on other sites More sharing options...
Ch0cu3r Posted February 26, 2015 Share Posted February 26, 2015 Adding on to cyberRobot's reply you will need run the query before the use of return $res; otherwise the query will never execute. This is because the return statement will terminate the function where it is called, anything after it will never be reachable. For more information please read http://php.net/manual/en/functions.returning-values.php http://php.net/return Link to comment https://forums.phpfreaks.com/topic/294914-call-a-function-from-a-function-and-update-mysql-db/#findComment-1506820 Share on other sites More sharing options...
vanquish12 Posted February 26, 2015 Author Share Posted February 26, 2015 thanks guys I'll give it a try, one more thing, my database connection details are in another file called mysql.php. I've added require("mysql.php"); to the top of the file however the $conn variable (which is set in the mysql.php file) is not being picked up by the function. Is there any way to pass this variable into the function or do I need to provide the connection details in every function which has to do something with the database? Link to comment https://forums.phpfreaks.com/topic/294914-call-a-function-from-a-function-and-update-mysql-db/#findComment-1506829 Share on other sites More sharing options...
Ch0cu3r Posted February 26, 2015 Share Posted February 26, 2015 Then you will need to pass it as an argument when you call the function. See the following for more info http://php.net/manual/en/functions.arguments.php Link to comment https://forums.phpfreaks.com/topic/294914-call-a-function-from-a-function-and-update-mysql-db/#findComment-1506833 Share on other sites More sharing options...
vanquish12 Posted February 26, 2015 Author Share Posted February 26, 2015 thanks for your help guys! I created a databaseConnect() function and I'm using that. Link to comment https://forums.phpfreaks.com/topic/294914-call-a-function-from-a-function-and-update-mysql-db/#findComment-1506871 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.