mythri Posted November 27, 2014 Share Posted November 27, 2014 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 Link to comment https://forums.phpfreaks.com/topic/292748-passimg-a-variable-to-a-function-and-getting-array-value-from-that-function/ Share on other sites More sharing options...
Barand Posted November 27, 2014 Share Posted November 27, 2014 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. Link to comment https://forums.phpfreaks.com/topic/292748-passimg-a-variable-to-a-function-and-getting-array-value-from-that-function/#findComment-1497829 Share on other sites More sharing options...
mythri Posted November 27, 2014 Author Share Posted November 27, 2014 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 Link to comment https://forums.phpfreaks.com/topic/292748-passimg-a-variable-to-a-function-and-getting-array-value-from-that-function/#findComment-1497861 Share on other sites More sharing options...
CroNiX Posted November 27, 2014 Share Posted November 27, 2014 Since it's being put into an array within a loop, you probably need to access each array using the index, like $info[0]['name'] Link to comment https://forums.phpfreaks.com/topic/292748-passimg-a-variable-to-a-function-and-getting-array-value-from-that-function/#findComment-1497863 Share on other sites More sharing options...
mythri Posted November 27, 2014 Author Share Posted November 27, 2014 Yes. It worked! Thanks a lot CroNiX and Barand Link to comment https://forums.phpfreaks.com/topic/292748-passimg-a-variable-to-a-function-and-getting-array-value-from-that-function/#findComment-1497866 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.