programmingsite : code , tutorials and scripts
Navigation

home


ASP
ASP.net
C
C++
C#
PHP
Java
JavaScript
Powershell
VB.net

Visual Basic


Resources
code
tutorials
training

Ads
Products

 

 

Description : Displaying data in a MySQL database using PHP




Displaying data in a MySQL database using PHP

First of all you have to create your MySQL database . To keep things simple I use PHPMyAdmin which offers a nice user friendly front end to MySQL . Create a database and call it anything you want , in this case maybe call it test.

Here is a SQL dump of the information that you require to add to your database

#
# Table structure for table `links`
#
# Creation: Aug 12, 2003 at 05:11 PM
# Last update: Aug 12, 2003 at 05:28 PM
#

CREATE TABLE `links` (
`id` int(10) unsigned NOT NULL auto_increment,
`url` varchar(100) NOT NULL default '',
`description` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=6 ;

#
# Dumping data for table `links`
#

INSERT INTO `links` VALUES (1, 'http://www.programmingsite.co.uk', 'programming directory');
INSERT INTO `links` VALUES (2, 'http://www.beginnersphp.co.uk', 'PHP tutorials and code');
INSERT INTO `links` VALUES (3, 'http://asp.programmershelp.co.uk', 'ASP site with code and tutorials');
INSERT INTO `links` VALUES (4, 'http://javascript.programmershelp.co.uk', ' javascript site');
INSERT INTO `links` VALUES (5, 'http://software.programmingsite.co.uk', ' software directory');

Now for the actual script itself , again you can call the script anything you want.

<?php
//connect to database using the mysql_connect function()
//This is your host and then your username and then finally your password
$db = mysql_connect("localhost","username","password");
//The function mysql_select_db() sets the current
//database to the one specified in this case "test"
mysql_select_db("test" ,$db);
//mysql_query() execute the specified MySQL query in this case
//we are are storing this in the variable $sql
$sql = mysql_query("SELECT * FROM links" ,$db);
//now we wil start producing a table
echo ("<table border ='1'>");
//this part displays our headings
echo ("<tr><td>url</td><td>description</td></tr>");
//the mysql_fetch_row() function fetches the next row in the resultset
//and stores this in an array , in this case $tablerows
while ($tablerows = mysql_fetch_row($sql))
{
//we now finish our table off by inserting values into each
//column . $tablerows[1] is equivalent to our url field and
//$tablerows[2] is the description field
echo("<tr><td><a href='$tablerows[1]'>$tablerows[1]</a></td><td>$tablerows[2]</td></tr> ");
}
echo "</table>";
//close database (good practice)
mysql_close($db);
?>



In action :

urldescription
http://www.programmingsite.co.ukprogramming directory
http://www.beginnersphp.co.ukPHP tutorials and code
http://asp.programmershelp.co.ukASP site with code and tutorials
http://javascript.programmershelp.co.uk javascript site
http://software.programmingsite.co.uk software directory





Products
500 Java Tips E-book
PHP editor
PERL editor
Beginning Java
Beginning Visual Basic
Learn VB.net
Learn VB 6
VB and databases
ASP image library
C++ builder programming
C++ fundamentals
Asp Studio 2005
Xtra PHP
Online PHP Script Encoder
Comparison

Links
Make money selling software
Uk shopping
UK price comparison
US price comparison
free articles
free stuff
Gambling resources
Software directory
XBox cheats and more
Sexy free wallpapers
Hosting resources
Nintendo Wii info
Beginners PHP
Health and fitness
Domain names
Dating site
Nintendo DS resources
Dropshippers
Maxi directory
Perfume and fragrances
PC shopping, tips and info
Magazine superstore

Ads