I am a php noob and have a quick question

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
echo 'Connected successfully';

I do not understand the if (!$link) part
what does the ! mean and what is the value of link?

thank you

Re: I am a php noob and have a quick question

Computer Guy wrote:

Quoted text here. Click to load it

If no connection has been set to the database, for any reason, throw an
error and die gracefully.

Quoted text here. Click to load it

http://php.net/mysql_connect (the part about "return values")

Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Siempre y nunca, es tan largo el uno como el otro.- Elsa Triolet.

Re: I am a php noob and have a quick question

On Mon, 18 Jun 2007 15:21:34 -0700, Computer Guy wrote:

Quoted text here. Click to load it

It's checking to see if $link has been assigned a valid connection. The
exclamation point means "not" or "false" however you'd like to turn it
into English.

Re: I am a php noob and have a quick question

It's pretty simple.  The PHP manual says this about the mysql_connect

"Returns a MySQL link identifier on success, or FALSE on failure."

So, if the connection to the database does not succeeds then the
condition !$link will equate to TRUE because the ! means NOT i.e. NOT
FALSE meaning TRUE.  If you had to translate this to English
therefore, the test !$link tests whether the database connection has
been made and if not, then it will run the code:

die('Could not connect: ' . mysql_error());

which causes the script to terminate giving an appropriate error

Hope this helps.


Re: I am a php noob and have a quick question

In our last episode,
talented Computer Guy broadcast on comp.lang.php:

Quoted text here. Click to load it

! 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.

Site Timeline