SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mysql queries not working inside php functions...

    I'm trying to call a mysql function inside a php function and it is not working. How do I pass a database connection to a function? See the code below that illustrates my problem.

    Thanks,
    Ken

    //PARENT FILE
    <?
    include '/functions/myfunction.php;

    $conn=mysql_connect($db_name, $db_user, $db_password);
    mysql_select_db($dbname);
    .
    .
    .
    $result=function_call($data1, $data2);

    mysql_close($conn);
    ?>
    //END PARENT FILE

    //START FUNCTION FILE
    function function_call($data1, $data2){
    $query=mysql_query("SELECT field FROM table WHERE data='$data1');
    $row=mysql_fetch_array($query);
    $field=$row['field']; // Field returns empty...
    }
    //END FUNCTION FILE

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try doing

    PHP Code:
    function function_call($data1$data2){
        
    $query=mysql_query("SELECT field FROM table WHERE data='$data1'");
        while(
    $row=mysql_fetch_array($query)) {
            
    $field=$row['field']; // Field returns empty...
        
    }


  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code itself is fully tested. I got it working in a single file before I split off the functions. The database connection seems to be out of the scope of the function calls. I don't know how to specify to the function which database connection to use.

  4. #4
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Try this
    PHP Code:
    function function_call($data1$data2){
    GLOBAL 
    $conn;
    $query mysql_query("SELECT field FROM table WHERE data='".$data1."'");
    $row mysql_fetch_array($query);
    $field $row['field']; // Field returns empty...

    Last edited by jelena; Jan 19, 2006 at 02:54. Reason: parse error fixed...
    -- Jelena --

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and building on what jelena said if you want to put the database connection in there you could also do

    $query=mysql_query("SELECT field FROM table WHERE data='$data1'",$conn);

  6. #6
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whitemank
    The code itself is fully tested. I got it working in a single file before I split off the functions. The database connection seems to be out of the scope of the function calls. I don't know how to specify to the function which database connection to use.
    if fthe cde is fully tested, then why does it have an unmatched double-quote in the mysql_query() line? posting modified or out-of-context code is rarely helpful.

  7. #7
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    If you omit the db connection resource from the mysql_query function it defaults to using the last opened connection.(http://us2.php.net/mysql_query)
    But it's good practice to include the db link identifier in the function call so things don't get screwed up later if you decide to open up another connection in your script.
    It would also help to know exactly what you want to achieve. What do you want the function return?


Bookmarks

Posting Permissions

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