gansai Posted July 9, 2018 Share Posted July 9, 2018 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); Link to comment 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. Link to comment Share on other sites More sharing options...
Barand Posted July 9, 2018 Share Posted July 9, 2018 ... 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? Link to comment 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? Link to comment 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. Link to comment 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 Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.