473,473 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

I am a php noob and have a quick question

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?

thank you

Jun 18 '07 #1
4 1549
Computer Guy wrote:
I do not understand the if (!$link) part
If no connection has been set to the database, for any reason, throw an
error and die gracefully.
what does the ! mean and what is the value of link?
http://www.php.net/manual/en/languag...rs.logical.php
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.
Jun 18 '07 #2
On Mon, 18 Jun 2007 15:21:34 -0700, Computer Guy wrote:
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?
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.

Jun 18 '07 #3
It's pretty simple. The PHP manual says this about the mysql_connect
function:

"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
message.

Hope this helps.

Shubeer

Jun 18 '07 #4
In our last episode,
<11**********************@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.
Jun 19 '07 #5

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

Similar topics

5
by: Tony Vasquez | last post by:
Hi fellas. I have a problem. I am trying ot get a popup window to appear in the center of the screen, both vertically and horozontally. I am trying to display a large version of an image in this...
8
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
2
by: Chris Dunaway | last post by:
I was looking over some asp.net code and saw this asp:Image control: <asp:Image ImageUrl="~/images/logo.gif" runat="server" ID="Image1"/> What does the tilde (~) mean in the ImageUrl...
1
by: Bruce | last post by:
Hello, Whats the difference b/w HTML control w/ ranat=server and "asp:" control ? Thanks.
13
by: cgough | last post by:
My true programming language is C++. I am at best a VB6 hacker that is just getting into VB.NET. I have a quick question about when to new and when not to new. Consider the following 2 classes....
4
by: foker | last post by:
I have an array with 50 elements in it, and a huge document with like 35,000 words on it. What I want to do is count the number of times each element has appeared in the document. This is what I...
3
by: scottevanshill | last post by:
I have this silly query in access 2003, i keep needing to remove certain id's from the result, but its gotten so big i'm getting error 3360 access, "query is to complex" This is the problem...
1
by: Demel, Jeff | last post by:
I'm having trouble finding exactly what I need by googling, so thought I'd try to get a quick answer from the group. This seems like something that should be dead simple. I need to generate a...
2
by: fieldsy34 | last post by:
I have a project due tomorrow and have a quick question. I use form load and input boxes to get input for 12 months of rainfall. I then use radio buttons to ask for different things such as total...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.