473,385 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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("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
Sep 5 '06 #1
3 1437

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.

Sep 5 '06 #2
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
Sep 5 '06 #3

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(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
Yes -- much better :)

Sep 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark | last post by:
I have a website with an increasing amount of articles and news reports and so I am thinking of moving away from storing each article as a seperate page to having a single page and storing articles...
9
by: pescott | last post by:
Hello people, I could really use your help on this one... I have script to upload image data to a database, but all that is uploaded is the decription, not the data BLOB itself. There must be...
3
by: Dave Smithz | last post by:
Hi There, Being quite new to MS-SQL I would like to ask if there is a general opinion of what approach should be taken to storing things like external documents and images in databases. ...
11
by: bissatch | last post by:
Hi, I am trying to upload an image, create a new file based on that image and then store the base64 encoded image data in a database. I dont really know where my code is going wrong so I will...
4
by: Rednelle | last post by:
Greetings all, As a newbie, using Access 2000, I would appreciate advice on the best way to include pictures. I have developed a 'Home Inventory' database which can include jpeg thumbnails of...
17
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to...
9
by: Brad | last post by:
I have written some code to manipulate data/records in a MASTER (order header) and DETAIL (order details) tables. What I have written is too extensive to post but essentially trying to: 1....
12
by: Joe Abou Jaoude | last post by:
hi, I have a component that uses a database connection. In the finalizer I dispose the connection because I read in msdn the following: "A type must implement Finalize when it uses...
5
by: Demetri | last post by:
Unfortunately, where I work it is forbidden to use stored procedures. I know, I know...it's pretty stupid. But I can not change that. Anyway, currently we have our sql stored in an embedded resx...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.