Gmail / Facebook Style Chat Application
Details Live Demo Download


Posted: Thursday 4th of October 2012

Finishing The Script on PHP and MySql



Introduction
Throughout this tutorial, I have taught you how to use PHP to interact with a MySql (or SQL) database and how to use the most common commands available. I have throughout this tutorial, also shown you how to create a basic contacts management system to illustrate some of the options you can use. In this part, I will show you some final MySql tips and will give you a final version of the script.

Saving Time
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:

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

Replacing the appropriate sections. Then in your php files use the following code at the top of the page:

include("dbinfo.inc.php");

or

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

at the top. 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.

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

The Finished Script
Throughout this tutorial, I have given you pieces of code to make a contacts database script. You can download the full script as a zip file so that you can examine the code (see Related Links).

Conclusion
From this tutorial, you should now know 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.

That's it guys...










Comments
0

 

Announcement x
Dear Users and Clients,

This is to inform you that we have recently updated our website generally.

If you experience any problem viewing any part of this website or if you notice that any of our scripts or tutorials are not functioning properly as should be, please click on the Contact Link at the top of this page to report the issue to us.

Our priority is to give you the best of service!


Thank You,
The Vasplus Team

Announcement