Posted 02.03.2010 | Updated 02.03.2010 | Contributed by Andy Mallett
Getting a PHP based page to dynamically access a MySQL database is a fine thing indeed and has scads of useful applications.
A big bonus is that it only requires a few lines of code in the page to access the MySQL database information. Data can also be added and deleted as required.
It is necessary to have PHP and MySQL installed and to be familiar with some basic MySQL commands before commencing.
|
|
Overachievers, short-attention-spanners and übergeeks may wish to go straight to the PHP/HTML code for connecting to MySQL.
* Note that all .txt files on this page should be copied and given .php file extensions before use *
As can be seen, the PHP connect requires the name of the MySQL server, plus a valid username and password for the specific MySQL database. In the example the database is called dvd and the table is called dvd1.
In this case the code pulls everything (*) from the DVD1 table and displays it in the required format in a standard HTML table. The result looks something line this (screenshot). Note the data is sorted by the ID field which is the key field.
Modify the order by command to sort data by different fields.
Modifying Data
Adding a record is achieved through a couple of pages. The first is a data insertion form which looks like this (screenshot). Note that the code calls another page, insert.php which actually inserts the data and echoes the results.
Finally, deleting a record is also achieved through another form which looks like this (screenshot) and deletes by line number (the unique identifier field). It also calls another page, delete.php which deletes data and echoes the results.
- A.
|
|