Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [236]

By Root 1557 0
”EN” dir=”ltr” xmlns=”http://www.w3.org/1999/xhtml”>

showContact.php

$conn = mysql_connect(“localhost“,“user“,“password“) or die(mysql_error());

mysql_select_db(“xfd“);

$sql = “SELECT * FROM contact“;

$result = mysql_query($sql, $conn) or die(mysql_error());

while($row = mysql_fetch_assoc($result)){

foreach ($row as $name => $value){

print “$name: $value
\n“;

} // end foreach

print “
\n“;

} // end while

?>

If you want to try this program at home, begin by running the buildContactAutoIncrement.sql script (available in Book VI, Chapter 2) in your copy of MySQL. This will ensure you have the database created. See Book VI, Chapter 2 if you need more information on creating databases.


Understanding data connections

The key to all database work is the connection. Database connections remind me of the pneumatic tubes at some bank drive-through locations. There’s a little container you can stick your request into. You press a button, and the container shoots through a tube to the teller, who processes your request and sends you the results back through the tube.

In data programming, the connection is like that tube: It’s the pipeline between your program (your car) and the data (the bank). To establish a data connection, you need to know four things:

♦ The hostname (where the server is): Often, the data server will be housed on the same physical machine as the Web server and PHP program. In these cases, you can use localhost as the server name. Test servers using XAMPP almost always use localhost connections. If you’re working in a production environment, you may need to ask your service provider for the server address of your database.

♦ Your database username: Database programs should always have some type of security enabled. (See Book VI, Chapter 1 for information on setting up database users and passwords.) Your program needs to know the username it should use for accessing the data. (I often create a special username simply for my programs. Book VI, Chapter 1 outlines this process.)

When you first install MySQL through XAMPP, it allows root access with no password. These settings allow anybody to do anything with your data. Obviously, that’s not a good solution, security-wise. Be sure to set up at least one username and password combination for your database. If you’re using an online hosting service, you probably don’t have root access. In this case, you typically have a new user created for each database you build. Book VI explains all.

♦ A password for the database: The username isn’t secure without a password. Your PHP program also needs a password. This is established when you create the database.

If you’re going to make your source code available (as I do on the companion CD and Web site), be sure to change the username and password so people can’t use this information to hack your live data.

♦ The database name: A single installation of MySQL can have many databases available. You’ll typically have a separate database designed for each project you build. MySQL needs to know which particular database houses the information you’re seeking.

Building a connection

The data connection is created with the mysql_connect() function. Here’s how to do it:

1. Create a variable to house the connection.

When you build a connection, a special variable is created to house information about that variable. I usually call my connection $conn:

$conn = mysql_connect(“localhost”,”user”,”password”) or die(mysql_error());

2. Invoke the mysql_connect() function.

This function (usually built into PHP) attempts to build a connection to the database given all the connection information:

$conn = mysql_connect(“localhost”,”user”,”password”) or die(mysql_error());

3. Pass the hostname, username, and password to mysql_connect().

These three values are required parameters of the mysql_connect()

Return Main Page Previous Page Next Page

®Online Book Reader