NEWS CATEGORIES:



NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
Home / News / Webmaster / Tips and Tricks

Tips and Tricks


A Universal PHP Script to Interact with Any MySQL Database

Connect to any MySQL database using a HTML form

By Catalin Bocanu, Web News Editor

15th of November 2007, 17:39 GMT

Adjust text size:


Tables Listing for A MySQL Database in phpMyAdmin
Enlarge picture
There are many situations when you need to connect to various MySQL databases, but at the same time you have to change the variables needed for the connection script. You can solve this situation by using connection variables that can be modified with the help of a HTML form. This form will allow you to send the new values of the variables to the
connection script.

Practically, you can connect to a MySQL database in various ways from PHP. In this article I propose a universal connection method that could be used in PHP/MySQL applications installation or could be integrated in any other PHP program that requires connections to different MySQL databases or servers.

The next listing (dbconnect.html) represents the HTML code of the form:

CODE
Database Host:

User Name:

Password:

Database Name:




For the default privileges values of any MySQL installation, the form fields corresponding to Database Host and respectively User Name must be filled in with localhost (127.0.0.1) and root. The Password field will stay empty, because the MySQL root account by default has no password assigned. In other situation, a connection with a remote MySQL server could be also established by specifying the server address and port in Database Host field.

In the Database Name field, you must insert the name of the database. In the next code listing (dbconnect.php), there are the PHP statements that form the connection to MySQL database script and a test query demonstrating the connection validity:

CODE

The function mysql_connect will establish the connection with the MySQL server by using the values of the connection variables ($dbhost,$user,$password)received from the HTML form. If the connection with the server is not established, then the error message "Connection refused by server" will be displayed. Finally, a database will be selected in order to perform queries on it (from variable value $dbname).

The sample query listed in the PHP script will output the names of all tables existing in the database. You can easily adapt this script to suit your own needs, as well as the HTML form. You can try to extend the script functionality by testing another type of queries for a certain MySQL database, such as view, add, edit, delete or search records. The script also represents a good start for a universal MySQL database interface. You should test the script by calling the HTML document, where you must fill in the form with your MySQL server and database connection variables values.

TAGS:

MySQL Database | Database Interface | Database Connection | MySQL Administration | Database Tool


Rating:
Good (3.8/5) 5 vote(s) so far    

Read by 1,828 user(s) | Add comment | Link to this article
Subscribe to news | Print article | Send to friend

© Copyright 2001-2008 Softpedia
Contact:

 

 

SEARCH THE NEWS ARCHIVE :




Today's News
| Yesterday's News | News Archive


MORE RELATED ARTICLES:


How to Publish a Database on the Internet

Could Login Passwords Be Cracked?

Organize Your Images in a MySQL Database

How to Install phpMyAdmin on Windows

Automatically Generate PHP / MySQL Websites

The Miracles of an Automatic Free Scripts Installer

Management of A Free Custom Search Engine

Is Joomla! The Best CMS on the Planet?

Tips to Improve Joomla Performance

User opinions:


Comment #1 by: deepthi on 31 Dec 2007, 03:56 GMT reply to this comment

got a lot of valid information.

Share your opinion:

Your Name:
Your Email Address:
(will not be used for commercial purposes)
Solve this to prove you're not a bot: =
Your review/opinion:

 






SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   ENTER NEWS SITE   |   ENGLISH BOARD   |   ROMANIAN FORUM