Jump to content

passimg a variable to a function and getting array value from that function


Go to solution Solved by mythri,

Recommended Posts

I want to define a function instead of repeating query in all my php pages. I call a function by passing an $id value and from that function i have to get all the info related to that id, like name, description and uom. 

 

I am trying to do this, but i dont know how to get these values seperately.

 

here is my function

 

function items($item_id)
{
$details = array(); 
$result = mysql_query("select item_id, name, uom, description from items where item_id=".$item_id."") or die (mysql_error());
while($row = mysql_fetch_array($result))
{
$details[] = array((stripslashes($row['name'])), (stripslashes($row['uom'])), (stripslashes($row['description'])));
}
return $details;
}

and i call my function like this

$info = items($id);

Can somebody guide me in this

 

 

 

As you have it now the name would be in $info[0], uom in $info[1] and description in $info[2]. I'd do it something like this

function items($item_id)
{
    $details = array(); 
    $result = mysql_query("select name, uom, description from items where item_id=".$item_id."") or die (mysql_error());
    while($row = mysql_fetch_assoc($result))
    {
        $details[] = $row;
    }
    return $details;
}

$info =  items($id);

echo $info['name'];
echo $info['uom'];
echo $info['description'];

If you are having to stripslashes then your handling of inputs to the database is wrong.

I tried this, but 

 
echo $info['name'];
echo $info['uom'];
echo $info['description'];

doesn't return any result :(.  if i use like this var_dump($info);  it shows the data. same thing was happening earlier also

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.