|
Posted by ZeldorBlat on 12/16/71 11:57
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("dbPassword", "xxx");
> define("dbDatabase", "blog");
>
> function dbConnect()
> {
> // Stores the database resource link in the global array
> $blog["db"] = mysql_connect(constant("dbHost"),
> constant("dbUser"),
> constant("dbPassword"))
> 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($blog["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.
Navigation:
[Reply to this message]
|