Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Help returning values from a function?

    Hi,

    I'm looking for some help please to write a function, which:

    1) Queries a database
    2) Could return ONE or MORE results
    3) Return either the ONE or MORE results comma seperated

    However, I can only get this to return one value, how would I get it to return multiple values?

    PHP Code:
    function findName($name) {
     
    $sql "select id from table WHERE name LIKE '%$name%'";
     
    $query mysql_query($sql);
     while(
    $result mysql_fetch_array($query) {
      
    $the_ids $result['id'];
    }
    return 
    $the_ids;

    So the above could return multiple ids eg:

    1,56,86,72

    Or simply just a single id eg:

    56

    Can this be done and can anyone kindly help please?

    Thanks
    Last edited by stfc_boy; 08-24-2011 at 03:52 PM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    function findName($name) {
     
    $sql "select id from table WHERE name LIKE '%$name%'";
     
    $query mysql_query($sql);
     while(
    $result mysql_fetch_array($query) {
      
    $the_ids[] = $result['id'];
    }
    return 
    $the_ids;

    PHP Code:
    $names=findName('some value');

    if(
    is_array($names))
      echo 
    $names=implode(',',$names ); 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    stfc_boy (08-24-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •