//, PHP/Creating and Accessing MySQL Data with PHP

Creating and Accessing MySQL Data with PHP

Creating and Accessing MySQL data with PHP

This tutorial will hopefully teach you some basics of how to access data with your PHP scripting to gather information with in a MySQL database.

The first step you need to do is get a basic understanding of what MySQL is. If your site can handle or has access to a MySQL database you should try using the PHPmyadmin application to help you use and access your MySQL datases fast and easy.

Once you are able to access PHPmyadmin, start off my creating a simple database.
In this case lets call it bubba.

Now that you have created your first database called bubba, lets add the table information.

Under the name filed enter the word love, and in the field area add in 3 for the fields, and press enter.

Now the next screen will ask you to create the data fields that you require for this new table called love.

In the first one enter in id, the second enter first, and the third enter in last.

For the type for id choose INT and the lenth of 11. Click onto the Primary for th id field. Now under the field Extra, choose the option to auto auto_increment. This is VERY important thing to do for auto assigning an ID number and is used for 75% of all scripts.
For the type for first choose VARCHAR and the length of 25.
For the type for last choose VARCHAR and the length of 25.

If this has been done correctly, you should see the following screen:

Lets add in some information to this database. To make is short and easy simply highlight and copy the following information.

Once you do this paste thie information you have copied above to the following area:

Click on to the go button, and the information will be added into the database name of buba, and the table name of love.

Accessing the information with PHP

Now you can use many different ways to access a MySQL database, and some are simple, while others are very advanced.
This one is going to try and just teach you the basics that should hopefully get you started in the right direction.

First step is to make two blank files into a folder of your choosing:

Copy and past the following information into the index.php file:

include_once ("./settings.php");
$link = mysql_connect($host,$user,$password);
echo '<table cellpadding=0 spacepadding=0 border=1 width=100%><tr>';
$result=mysql_db_query($base,"select * from ".$basesites." WHERE first='darren'",$link);
if ($result > 0)
while ( $n<$num4 )
# call and show the ID
# call and show the first
# call and show the last
if (!$size) $size="0 kb";
echo "<tr>";
echo "<td align=left>$id</td>";
echo "<td align=left>$first</td>";
echo "<td align=left>$last</td>";
echo "</tr>";
echo "</table>";

Copy and past the following information into the settings.php file:

Now if you wanted to only acces a certain field you could change the index.php file looking like this:
$result=mysql_db_query($base,"select * from ".$basesites."",$link);
and add in WHERE first=’darren’ between ".$basesites." HERE ",$link lie the following:
$result=mysql_db_query($base,"select * from ".$basesites." WHERE first=’darren’",$link);

This would result in the php to only grab the following information:

I hope this helps you get some of the basics of writting a PHP script, and accessing a MySQL database.

2010-05-26T10:52:04+00:00 June 20th, 2005|MySQL, PHP|0 Comments

About the Author:

Darren Hedlund is a freelance Web developer, writer, and data analyst. Darren has a degree in Computer Information Science and has spent the last 15 years developing application and environments from hand held, windows, web, virtual science, gaming, artificial intelligence and graphics design.

Darren's coding knowledge ranges from C+, Visual Basic, .NET, PHP, JSP, REXX, KIXX, and many others. His graphical and environmental knowledge stems in Macromedia Flash, 3D studio Max, Curious Labs Poser, Adobe Photoshop, and many others. Darren works in many platforms ranging from database, visual design, and, system development.

Leave A Comment