SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member hightex's Avatar
    Join Date
    Nov 2002
    Location
    The Country
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Curious...function vs. no function

    I am curious...

    I am developing a scripts that simply updates a row in a MySQL table. I wrote the following function to do the trick...

    PHP Code:

    function updSql($vid)
        {
            
    $query "UPDATE video SET " 
                      
    $vid " = NULL 
                      WHERE login_id = " 
    $_SESSION["login_id"] . "";

            
    // Connect to DBMS
            
    if (!($connection = @ mysql_connect($hostname
                                                
    $username
                                                
    $password)))
                
    showerror();

            if (!
    mysql_select_db($databaseName$connection))
                
    showerror();

            
    // Execute the query
            
    if (!($result = @ mysql_query($query$connection)))
                
    showerror();
            
            
    mysql_close($connection);
        }


    session_start();
        
    $vidpath "/home/xxxxx/public_html/rsrr/dev/video/";
        foreach (
    $_POST as $varname => $value)
            
    $varname $vidpath $varname ".wmv";
        
        
    $vidfile strrchr($varname"/");
        
    $vidroot str_replace("/"""$vidfile);
        
    $vidroot2 str_replace(".wmv"""$vidroot);

        
    updSql($vidroot2); 
    When I run the script, I get a "No Database Selected" error. However, if I nix the function, and just run the exact code that exists in the function, it works! For the time being, I am just going without the func, but if anyone has any ideas on why this doesn't work, I'm all ears. By the way, I have the exact same DBMS connectivity in other functions, and they run fine!

    Wierd...

  2. #2
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see where you are actually connecting to the db within the function.
    Last edited by Jump; Dec 27, 2002 at 18:00.

  3. #3
    SitePoint Member hightex's Avatar
    Join Date
    Nov 2002
    Location
    The Country
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right here...

    PHP Code:
    // Connect to DBMS
            
    if (!($connection = @ mysql_connect($hostname
                                                
    $username
                                                
    $password)))
                
    showerror();

            if (!
    mysql_select_db($databaseName$connection))
                
    showerror(); 
    I forgot to state that I have an include file that contains the values for $hostname, $username, $password and $databaseName.

  4. #4
    Probably eating pie mitsubishi's Avatar
    Join Date
    Sep 2001
    Location
    England, UK
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where is $databaseName (or $hostname, $username and $password) coming from? Don't forget you are in a function, so base scope variables arn't automatically available. You would have to start the function like this:
    PHP Code:
    function updSql($vid)
    {
        GLOBAL 
    $databaseName$hostname$username$password;
        GLOBAL 
    $connection// You check for it 

  5. #5
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes The function will not just connect because your script has allready connect. Plus those commands you show are not connecting but rather descideing what is to be done if !connected.

  6. #6
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Strongly recommend not using globals like this;

    PHP Code:
        GLOBAL $databaseName$hostname$username$password;

        GLOBAL 
    $connection// You check for it 
    For reasons that become clearer the wiser you get with PHP, doing this is a bad idea which will end in nightmare code.

    Here's what I'd recommend doing;

    PHP Code:
    <?php
    // Builds and returns a database connection
    function & connectToDb ($hostname,$username,$password,$databaseName) {
        
    // Connect to DBMS
        
    if (!($connection = @ mysql_connect($hostname,$username,$password)))
            
    showerror();

        
    // SELECT database
        
    if (!mysql select db($databaseName$connection))
            
    showerror();

        return 
    $connection;
    }

    // Performs SQL
    function_updSql(&$connection$vid) {
        
    $query "UPDATE video SET " .
            
    $vid." = NULL WHERE login_id = " $_SESSION["login_id"] . "";

        
    // Execute the query
        
    if (!($result = @ mysql_query($query$connection)))
            
    showerror();
    }

    session_start();

    $vidpath "/home/xxxxx/public_html/rsrr/dev/video/";

    foreach (
    $_POST as $varname => $value)
        
    $varname $vidpath $varname ".wmv";

    $vidfile strrchr($varname"/");

    $vidroot str_replace("/"""$vidfile);

    $vidroot2 str_replace(".wmv"""$vidroot);

    // Connect to database
    $connection = & connectToDb ($hostname,$username,$password,$databaseName);

    // Pass connection to query function
    updSql($connection$vidroot2);
    ?>
    If you're wondering the about & symbols, I've tried to summarize this as best I can in this article.
    Last edited by HarryF; Dec 27, 2002 at 18:42.

  7. #7
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to be picky Harry, and off topic hightex, but I found a flaw in your artical


    PHP Code:
    <?php
    class Message {
        var 
    $msg 'Hello World';
    That doesn't work anymore

    I learned that the hard way, with a screwy database abstraction layer I made !

  8. #8
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Suggest you check out this http://www.php.net/manual/en/language.oop.php;

    Note: In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non- constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below).
    As I'm initializing $msg with a constant value that no problem. It's the following that are not allowed;

    PHP Code:
    <?php
    /* None of these will work in PHP 4. */
    class Cart
    {
        var 
    $todays_date date("Y-m-d");
        var 
    $name $firstname;
        var 
    $owner 'Fred ' 'Jones';
    }
    Note that;

    PHP Code:
        var $items = array("VCR""TV"); 
    ...seems to work fine (despite what the manual says).

  9. #9
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, sorry, that's the problem, I was defining it to a variable


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
  •