You are here: Re: Storing database resource links « PHP Programming Language « IT news, forums, messages
Re: Storing database resource links

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация