Jump to content

Extract database values


gansai

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.