gansai Posted July 9, 2018 Share Posted July 9, 2018 (edited) I was trying to extract all the records from sql query, it shows only the last record, $result is in for loop. function no_act(dept_id) { $noact = mysqli_query('SELECT deptid, act_name FROM dept WHERE act_id='.$actid.''); foreach($noact as $n) { $result = $n->act_name; } return $result; } no_act(dept_id); Edited July 9, 2018 by gansai Quote Link to comment https://forums.phpfreaks.com/topic/307473-extract-database-values/ Share on other sites More sharing options...
maxxd Posted July 9, 2018 Share Posted July 9, 2018 It shouldn't be showing anything at all. You're not using the dollar sign on either dept_id, then you're not using dept_id and you're making up $actid out of apparently nowhere. In addition to that, you're overwriting the value of $result on each iteration through the foreach loop, meaning that even if everything worked correctly and there were no typos in the code, you'd only expect to get the last result and the code is doing exactly what you're asking it to do. Quote Link to comment https://forums.phpfreaks.com/topic/307473-extract-database-values/#findComment-1559481 Share on other sites More sharing options...
Barand Posted July 9, 2018 Share Posted July 9, 2018 (edited) ... and $n will not be an object. Also there isn't a db connection in sight. So why don't you post your actual code instead of wasting our time with something that won't possiby run? Edited July 9, 2018 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/307473-extract-database-values/#findComment-1559483 Share on other sites More sharing options...
ginerjm Posted July 9, 2018 Share Posted July 9, 2018 And - what's with the two quotes at the end of your query statement? Quote Link to comment https://forums.phpfreaks.com/topic/307473-extract-database-values/#findComment-1559484 Share on other sites More sharing options...
gansai Posted July 10, 2018 Author Share Posted July 10, 2018 16 hours ago, maxxd said: It shouldn't be showing anything at all. You're not using the dollar sign on either dept_id, then you're not using dept_id and you're making up $actid out of apparently nowhere. In addition to that, you're overwriting the value of $result on each iteration through the foreach loop, meaning that even if everything worked correctly and there were no typos in the code, you'd only expect to get the last result and the code is doing exactly what you're asking it to do. I was trying to extract all the records from sql query, it shows only the last record, $result is in for loop. $actid = required_param('id', PARAM_INT); function no_act($dept_id) { $noact = mysqli_query('SELECT deptid, act_name FROM dept WHERE act_id='.$actid.''); foreach($noact as $n) { $result = $n->act_name; } return $result; } no_act($dept_id); I agree there was no $ for the variable.(edited the code). As I couldn't post the entire code, rather I was just looking for logic. I want to extract all the values from the query. When I call no_act($dept_id); it shows only the last record. Let me know where am I going wrong in my code. Quote Link to comment https://forums.phpfreaks.com/topic/307473-extract-database-values/#findComment-1559502 Share on other sites More sharing options...
Barand Posted July 10, 2018 Share Posted July 10, 2018 18 hours ago, maxxd said: you're overwriting the value of $result on each iteration through the foreach loop, meaning that even if everything worked correctly and there were no typos in the code, you'd only expect to get the last result Quote Link to comment https://forums.phpfreaks.com/topic/307473-extract-database-values/#findComment-1559503 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.