|
Posted by Lars Eighner on 06/18/07 23:59
In our last episode,
<1182205294.671165.187750@m36g2000hse.googlegroups.com>, the lovely and
talented Computer Guy broadcast on comp.lang.php:
> Hi I have recently started working with PHP and am making a
> neighborhood website. I am connecting to my Mysql database and am
> having difficulty understanding what is happening in the example on
> the PHP website. It connects fine but I do not understand the code.
> I do not just like to take other people's code so if someone can
> explain this to me that would be great.
><?php
> $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
> if (!$link) {
> die('Could not connect: ' . mysql_error());
> }
> echo 'Connected successfully';
> mysql_close($link);
> ?>
> I do not understand the if (!$link) part
> what does the ! mean and what is the value of link?
! means not (in PHP and many other programming languages).
!$link is true if $link is false, and !$link is false if $link is true.
In PHP variables which do not exist, are empty, or are equal to zero are
false, and otherwise are true. If mysql_connect fails it returns the
value false, so $link = FALSE, but if it succeeds it returns something
else which is not false (and therefore is true).
So essentially "if (!$link)" means "if $link does not exist." In some
contexts you must tell the difference between "does not exist" and
"exists but is zero" (which this test cannot distinguish), but this is not
one of those contexts.
$link, when a connection is established, is a resource id. You could echo
it out to look at but it would only look something like Resource id #11 (or
some other number). It is meaningless to you and is really a sort of handle
for you to refer to this particular database connection. Don't try to do
stuff with resources directly. That is, trying to set $link equal to the
string "Resource id #11" is likely to be disappointing. You can return
$link and in some other respects treat it like a variable if you don't muck
with its innards. And indeed, this is a case in point: if(!$link) treats
$link just like a boolean variable. You could OR it or AND it or assign it.
Just do not expect things to turn out well if you try to perform arithmetic
or string functions on it.
If you look up the mysql functions you will discover that some of them have
an optional argument [resource_link_identifier]. Well, it isn't really
optional, but if you leave it out, it reverts to the last link reference.
In other words, you will see code with a sequence of database operations and
you seldom see the link resource (almost always called $link) except for
when it is opened, tested, and closed. But it actually is there (as the
default) in mysql_selectdb, mysql_query, and so forth (and they will fail if
you have not previously established a link).
If $link was the last link you referred to, mysql_query($query) really
means mysql_query($query,$link) --- and vice versa, you always can
make the link resource explicit.
The link resource doesn't participate in mysql functions that act on
results, but some functions do not produce a result resource and so tests on
the link must be done (such as mysql_affected_rows) to determine the effect.
You could have several links open at once. You might select one database
for one link and a different one for another. Most of the time that would
be a sloppy and careless thing to do, but it is there when you really need
it or even if you don't. If you do open several links at once, you almost
certainly should make the link explicit for your own sanity, otherwise the
Law of General Cussedness will ensure that the default will always be to the
wrong link.
--
Lars Eighner <http://larseighner.com/> <http://myspace.com/larseighner>
Countdown: 581 days to go.
An amazing thing about Christians: people who doubt being related to monkeys,
but are certain they belong to the same species as Paris Hilton or Karl Rove.
Navigation:
[Reply to this message]
|