www.webdeveloper.com
Results 1 to 3 of 3

Thread: Specifying The Link In A Query Within A Function

  1. #1
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700

    Specifying The Link In A Query Within A Function

    I'm performing a query within a function.

    First, this works...
    PHP Code:
    function getdata($a$b)
    {
      
    $query "SELECT * FROM ".$a." WHERE rn = '$b'"//where $a is the data file name
      
    $result mysql_query($query);
      
    $rows mysql_fetch_array($result);
      return 
    $rows;

    ...but this does not;
    PHP Code:
    function getdata($a$b)
    {
      
    $query "SELECT * FROM ".$a." WHERE rn = '$b'";
      
    $result mysql_query($query$link);
      
    $rows mysql_fetch_array($result);
      return 
    $rows;

    The variable $link is declared as a global.

    I need to specify $link because there are two data resources from which the query could draw data and I need to specify which to use.

    Any thoughts?
    Oh Lord, please help me be the person my dog thinks I am.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Make it an additional function parameter:
    PHP Code:
    function getdata($a$b$link)
    {
      
    $query "SELECT * FROM ".$a." WHERE rn = '$b'";
      
    $result mysql_query($query$link);
      
    $rows mysql_fetch_array($result);
      return 
    $rows;

    You could declare it as global within the function, but that is generally frowned upon as bad coding style as it closely couples the function with the application, making it less re-usable and also leading to potential maintenance and testing issues.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    Thanks NogDog.
    Much appreciated as always.
    CTB
    Oh Lord, please help me be the person my dog thinks I am.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles