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 12/16/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

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация