Results 1 to 3 of 3

Thread: Using PHP function call seems to create problem with MySQL query

  1. #1
    Join Date
    Feb 2006
    Location
    North of Boston, MA
    Posts
    1,658

    Question Using PHP function call seems to create problem with MySQL query

    Interesting dilemma found calling a query in a function...

    I have a query that I call multiple times in a file. It reads like this:

    Code:
    <?php
    
      $dbh = mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
      mysql_select_db ($mydb);
    
      function list($title){
    
        $query = "SELECT * FROM table t WHERE t.title = '$title' ORDER BY t.year, t.last";
        $result = mysql_query($query,$dbh) or die(mysql_error());
    
    ...
    
      }
    
      list("Most Valuable Player");
      list("Rookie of the Year");
    
    ?>
    I get "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in [file] on line 9"

    Obviously, this usually means that there is an error in the query. However, if I run the same code without the function call as follows, I get no error.

    Code:
    <?php
    
      $dbh = mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
      mysql_select_db ($mydb);
    
      $title = "Most Valuable Player";
    
      $query = "SELECT * FROM table t WHERE t.title = '$title' ORDER BY t.year, t.last";
      $result = mysql_query($query,$dbh) or die(mysql_error());
    
    ...
    
    ?>
    Do I need to do something special when I perform a SQL query through a function call?
    redsox9 - Go Red Sox!!! 2004 and 2007 World Series Champions!
    Visit FenwayFanatics.com, home to Boston Red Sox baseball fans everywhere... now on Facebook and Twitter!

  2. #2
    Join Date
    Nov 2006
    Location
    Sydney, Australia
    Posts
    4,951

    Default

    The function has no access to global variables unless you either pass it to the function or define it as global.

    Either:

    Code:
    <?php
    
      $dbh = mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
      mysql_select_db ($mydb);
    
      function list($title){
    global $dbh;
    
        $query = "SELECT * FROM table t WHERE t.title = '$title' ORDER BY t.year, t.last";
        $result = mysql_query($query,$dbh) or die(mysql_error());
    
    ...
    
      }
    
      list("Most Valuable Player");
      list("Rookie of the Year");
    
    ?>
    or

    Code:
    <?php
    
      $dbh = mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
      mysql_select_db ($mydb);
    
      function list($title,$dbh){
    
        $query = "SELECT * FROM table t WHERE t.title = '$title' ORDER BY t.year, t.last";
        $result = mysql_query($query,$dbh) or die(mysql_error());
    
    ...
    
      }
    
      list("Most Valuable Player",$dbh);
      list("Rookie of the Year",$dbh);
    
    ?>

  3. #3
    Join Date
    Feb 2006
    Location
    North of Boston, MA
    Posts
    1,658

    Default

    Ah, I figured it was something to do with globals but I completely missed that. Thanks!
    redsox9 - Go Red Sox!!! 2004 and 2007 World Series Champions!
    Visit FenwayFanatics.com, home to Boston Red Sox baseball fans everywhere... now on Facebook and Twitter!

Tags for this Thread

Posting Permissions

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