Part 8 - Finishing The Script



Throughout this lab we have explored how to use PHP to interact with a MySQL (or SQL) database and how to use the most common commands available. We have, throughout this lab, also explored how to create a basic contacts management system to illustrate some of the options you can use. In this part we will consider some final MySQL tips and will give you a final version of the script.

When creating complex scripts using databases you will find that the most common thing you are doing is connecting to a database. Because of this, you can actually save time by creating either a username/password file or a connection file. For example for a username/password file you would create a file called:

dbinfo.inc.php

and put the following in it:

<?
$username="databaseusername";
$password="databasepassword";
$database="databasename";
?>

Replacing the appropriate sections. Then in your php files use the following code:

include("dbinfo.inc.php");

or

include("/full/path/to/file/dbinfo.inc.php");

at the beginning. Then, you can use the variables $username, $password and $database throughout your scripts without having to define them every time. Also, if you ever change this information, for example if you move to another web host, there is only one file to change.

You can use the same principal to connect to the database, by putting the connection code in the file, but you must always be sure to close the connection in each file or you may have problems with your MySQL server.



A limited form of searching can also be performed on your database using a built in MySQL function. This is by using the LIKE function as follows:

SELECT * FROM tablename WHERE fieldname LIKE '%$string%'

To explain furhter, LIKE tells the database to perform its 'searching' feature. The % signs mean that any other data could appear in their place and $string would hold your search string. In this place could be a word or number as well e.g.:

LIKE '%piano%'

which would output any rows with piano in the specified field.

Similarly, you can leave out one of the % signs so that you can specify the position of the string e.g.:

LIKE 'piano%'

Will only output rows where the specified field begins with piano, so:

The piano is next to the table.

Would not show up.

From this lab you should now understand the basics of using PHP and MySQL together to create database-enabled websites and programs. Using databases with the web opens up a huge new selection of things you can do and can make a simple website much more powerful, saving time updating the site, allowing user interaction and feedback and much more.


Part 1 - Introduction
Part 2 - Setting Up The Database
Part 3 - Inserting Information
Part 4 - Displaying Data
Part 5 - More Outputs
Part 6 - Single Records & Error Trapping
Part 7 - Updating & Deleting
Part 8 - Finishing The Script


Topic revision: r1 - 2011-11-11 - JimSkon
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback