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

Posted by Ney Andr� de Mello Zunino on 06/20/13 11:57

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(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());
return $conn;
}

function dbDisconnect($conn)
{
mysql_close($conn);
}

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

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

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

Cheers!

--
Ney André de Mello Zunino

 

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

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