Web Analytics Made Easy -
StatCounter Help returning values from a function? - CodingForum

Announcement

Collapse
No announcement yet.

Help returning values from a function?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Resolved 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; Aug 24, 2011, 10:52 AM.

  • #2
    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 ); 
    Digitalocean Cloud Hosting (Referral link - get $10 free credit) Fameco

    Comment

    Working...
    X
    😀
    🥰
    🤢
    😎
    😡
    👍
    👎