Balderick Posted April 3, 2017 Share Posted April 3, 2017 (edited) Hi All, I'm looking for a solution in using a function. The function fetches data from a database. In a column multiple results are found. I want to have all the records outisde the function. Inside the function all records are shown, but outside only the last record. I thought I could use return array, but it only returns multiple variables and not multiple rows. Not sure if fetch() is the solution for arrays. example: <?php function test_func($val4) { global $val1; global $val2; global $val4; $servername = "host"; $username = "johndoe"; $password = "admin"; $dbname = "dbase_db"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // if ($stmt = $conn->prepare("SELECT col1, col2 FROM table WHERE col4 = ? ; ")) { $stmt->bind_param("s", $val4); $stmt->execute(); $stmt->store_result(); $num_of_rows = $stmt->num_rows; var_dump($num_of_rows); $stmt->bind_result($val1, $val2); if ($num_of_rows > 0){ while ($stmt->fetch()) { // managed to output multiple rows here, but that's not the goal } return array ($val1, $val2); } } } $val4='form_value'; test_func($val4); // return multiple rows here, outside the function How should I solve this? Edited April 3, 2017 by Balderick Link to comment Share on other sites More sharing options...
dsonesuk Posted April 3, 2017 Share Posted April 3, 2017 Where ever you call the function, it should output the output from within the function, but depending on how the output is created, determines if the call of the function requires a echo. Link to comment Share on other sites More sharing options...
Balderick Posted April 3, 2017 Author Share Posted April 3, 2017 1 hour ago, dsonesuk said: Where ever you call the function, it should output the output from within the function, but depending on how the output is created, determines if the call of the function requires a echo. why should the output be called from within the function? Link to comment Share on other sites More sharing options...
john_jack Posted April 3, 2017 Share Posted April 3, 2017 (edited) You can store the query results in a multidimensional array check this https://www.w3schools.com/php/func_array.asp "example 4" you can then return the array . Edited April 3, 2017 by john_jack typo 1 Link to comment Share on other sites More sharing options...
dsonesuk Posted April 3, 2017 Share Posted April 3, 2017 Never said call from within function, anywhere outside the function within body of page. Depending on type of result you could simply concatenate the results then return. Simplerfied example $join=""; function dothis(){ for($i=0;$i<10;$i++){ $join.='Hello World '.$i.'<br>'; } return $join; } Then anywhere in body echo the function echo dothis(); Link to comment Share on other sites More sharing options...
Balderick Posted April 3, 2017 Author Share Posted April 3, 2017 I added this code in the loop : while ($stmt->fetch()) { $array_of_val1 = array($val1); var_dump($array_of_val); } but the result is that the both the offsets appear to be 0 and not 0 and 1 . Link to comment Share on other sites More sharing options...
Balderick Posted April 3, 2017 Author Share Posted April 3, 2017 (edited) Wait ................ I saw the light after adding brackets to $array_of_val1 thanks for the help . Edited April 3, 2017 by Balderick Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now