Storing database resource links

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ney André de Mello Zunino

    Storing database resource links

    Hello.

    Could anyone explain to me why the following code fails?

    // =====Begin sample code=====

    global $blog;

    define("dbHost" , "localhost" );
    define("dbUser" , "ney");
    define("dbPassw ord", "xxx");
    define("dbDatab ase", "blog");

    function dbConnect()
    {
    // Stores the database resource link in the global array
    $blog["db"] = mysql_connect(c onstant("dbHost "),
    constant("dbUse r"),
    constant("dbPas sword"))
    or die("DB connection error: " . mysql_error());
    mysql_select_db ('blog')
    or die("Error selecting blog database: " . mysql_error());
    }

    function dbDisconnect()
    {
    // Closes the connection represented by the resource link stored
    // in the global array
    mysql_close($bl og["db"]);
    }

    dbConnect();
    ..
    ..
    ..
    dbDisconnect();

    // =====End sample code=====

    Upon runtime, PHP produces the following warning:

    Warning: mysql_close(): supplied argument is not a valid MySQL-Link
    resource in /home/ney/Blog/db.php on line 22

    I would appreciate if anyone could point out my mistake and kindly
    explain the situation.

    Thank you.

    --
    Ney André de Mello Zunino
  • ZeldorBlat

    #2
    Re: Storing database resource links


    Ney André de Mello Zunino wrote:
    Hello.
    >
    Could anyone explain to me why the following code fails?
    >
    // =====Begin sample code=====
    >
    global $blog;
    >
    define("dbHost" , "localhost" );
    define("dbUser" , "ney");
    define("dbPassw ord", "xxx");
    define("dbDatab ase", "blog");
    >
    function dbConnect()
    {
    // Stores the database resource link in the global array
    $blog["db"] = mysql_connect(c onstant("dbHost "),
    constant("dbUse r"),
    constant("dbPas sword"))
    or die("DB connection error: " . mysql_error());
    mysql_select_db ('blog')
    or die("Error selecting blog database: " . mysql_error());
    }
    >
    function dbDisconnect()
    {
    // Closes the connection represented by the resource link stored
    // in the global array
    mysql_close($bl og["db"]);
    }
    >
    dbConnect();
    .
    .
    .
    dbDisconnect();
    >
    // =====End sample code=====
    >
    Upon runtime, PHP produces the following warning:
    >
    Warning: mysql_close(): supplied argument is not a valid MySQL-Link
    resource in /home/ney/Blog/db.php on line 22
    >
    I would appreciate if anyone could point out my mistake and kindly
    explain the situation.
    >
    Thank you.
    >
    --
    Ney André de Mello Zunino
    You need to put global $blog; inside your functions. This tells PHP to
    "bind" the variable $blog inside the function to the global one.
    Adding that should fix it...although personally I prefer not to use
    globals.

    Comment

    • Ney André de Mello Zunino

      #3
      Re: Storing database resource links

      ZeldorBlat wrote:
      You need to put global $blog; inside your functions. This tells PHP to
      "bind" the variable $blog inside the function to the global one.
      Adding that should fix it...although personally I prefer not to use
      globals.
      Thanks for your quick reply. You are right: globals are evil, at least
      most of the time. I reworked the code, which now looks like:

      // =====Begin sample code=====

      function dbConnect()
      {
      $conn = mysql_connect(c onstant("dbHost "),
      constant("dbUse r"),
      constant("dbPas sword"))
      or die("DB connection error: " . mysql_error());
      mysql_select_db ('blog')
      or die("Error selecting blog database: " . mysql_error());
      return $conn;
      }

      function dbDisconnect($c onn)
      {
      mysql_close($co nn);
      }

      $blog["dbconn"] = dbConnect();
      ..
      ..
      ..
      dbDisconnect($b log["dbconn"]);

      // =====End sample code=====

      I guess it has improved a bit, hasn't it?

      Cheers!

      --
      Ney André de Mello Zunino

      Comment

      • ZeldorBlat

        #4
        Re: Storing database resource links


        Ney André de Mello Zunino wrote:
        ZeldorBlat wrote:
        >
        You need to put global $blog; inside your functions. This tells PHP to
        "bind" the variable $blog inside the function to the global one.
        Adding that should fix it...although personally I prefer not to use
        globals.
        >
        Thanks for your quick reply. You are right: globals are evil, at least
        most of the time. I reworked the code, which now looks like:
        >
        // =====Begin sample code=====
        >
        function dbConnect()
        {
        $conn = mysql_connect(c onstant("dbHost "),
        constant("dbUse r"),
        constant("dbPas sword"))
        or die("DB connection error: " . mysql_error());
        mysql_select_db ('blog')
        or die("Error selecting blog database: " . mysql_error());
        return $conn;
        }
        >
        function dbDisconnect($c onn)
        {
        mysql_close($co nn);
        }
        >
        $blog["dbconn"] = dbConnect();
        .
        .
        .
        dbDisconnect($b log["dbconn"]);
        >
        // =====End sample code=====
        >
        I guess it has improved a bit, hasn't it?
        >
        Cheers!

        --
        Ney André de Mello Zunino
        Yes -- much better :)

        Comment

        Working...