Low cost ecommerce web development India
flash website design
Connecting to MySQL with PHP
Before you can get content out of your MySQL database for inclusion in a Web
page, you must first know how to establish a connection to MySQL from inside
a PHP script. Back in Chapter 2, you used a program called mysql that allowed
you to make such a connection. PHP has no need of any special program, however;
support for connecting to MySQL is built right into the language. The following
PHP function call establishes the connection:
mysql_connect(address, username, password);
Here, address is the IP address or host name of the computer on which the
MySQL server software is running ("localhost" if it's running on the
same
computer as the Web server software), and username and password are the same
MySQL user name and password you used to connect to the MySQL server in
Chapter 2.
You may remember that functions in PHP usually return (output) a value when
they are called. Don't worry if this doesn't ring any bells for you—it's
a detail that
I glossed over when I first discussed functions. In addition to doing something
useful when they are called, most functions output a value, and that value may
be stored in a variable for later use. The mysql_connect function shown above,
for example, returns a number that identifies the connection that has been established.
Since we intend to make use of the connection, we should hold onto this
value. Here's an example of how we might connect to our MySQL server.
$dbcnx = mysql_connect('localhost', 'root', 'mypasswd');
As described above, the values of the three function parameters may differ for
your MySQL server. What's important to see here is that the value returned by
mysql_connect (which we'll call a connection identifier) is stored in a variable
named $dbcnx.
As the MySQL server is a completely separate piece of software, we must consider
the possibility that the server is unavailable or inaccessible due to a network
outage, or because the username/password combination you provided is not ac-
71
Connecting to MySQL with PHP
cepted by the server. In such cases, the mysql_connect function doesn't return
a connection identifier, as no connection is established. Instead, it returns
false.
This allows us to react to such failures using an if statement:
$dbcnx = @mysql_connect('localhost', 'root', 'mypasswd');
if (!$dbcnx) {
echo( '<p>Unable to connect to the ' .
'database server at this time.</p>' );
exit();
}
There are three new tricks in the above code fragment. First, we have placed
an
@ symbol in front of the mysql_connect function. Many functions, including
mysql_connect, automatically display ugly error messages when they fail. Placing
the @ symbol (also known as the error suppression operator) in front of the
function name tells the function to fail silently, allowing us to display our
own,
friendlier error message.
Next, we put an exclamation point in front of the $dbcnx variable in the condition
of the if statement. The exclamation point is the PHP negation operator, which
basically flips a false value to true, or a true value to false. Thus, if the
connection
fails and mysql_connect returns false, !$dbcnx will evaluate to true, and cause
the statements in the body of our if statement to be executed. Alternatively,
if
a connection was made, the connection identifier stored in $dbcnx will evaluate
to true (any number other than zero is considered "true" in PHP),
so !$dbcnx
will evaluate to false, and the statements in the if statement will not be executed.
website designer freelance ASP PHP ecommerce web developer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110