Jump to content

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);

 

Edited by gansai
Link to comment
https://forums.phpfreaks.com/topic/307473-extract-database-values/
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.

... 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 by Barand
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.

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.