Jump to content

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


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

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.