iWebBuddy.com
Thanks for visiting iWebBuddy.com ,

If it is your first time here, please enjoy reading the tutorials and articles. If you can not find what your looking for, then you can join the forums by registering so you will be able to log into the forums and ask for support for website development, request graphic designing services, promote your website, request website reviews, get advice from others about your website or be an active member of the community to help iWebBuddy.com grow.

After you register an account, you are required to log in. Once you log into your account, you will have more previliges. You will be able to post on the forums, send private messages and view the Chatbox ( link ) at the bottom of the forums.

Have a nice day.


Connect with a Database

Post new topic   Reply to topic

View previous topic View next topic Go down

Connect with a Database

Post by dsignresponder on Wed Jan 20, 2010 12:59 pm

Hi ALL lovers of server-side scripting languages!
I'm sorry for being inactive for a couple of weeks, I have no excuse for that... Actually I was very busy with a new PHP project but I do believe that IS NOT an excuse...

Anyway, I'd like to show you one of the main powers of PHP :
connection to a database and reading table content!

If you installed Apache or XAMPP as I described HERE, you already have mysql database support at your local 'php workshop'! Very Happy

Now point your browser to
Code:

http://localhost/phpmyadmin/


and create a database!
To create a table in a database you can use 2 methods :
1. manually inserting table name, table fields and type at the phpMyadmin administrator panel (link above)
2. you can write a simple PHP script for that and then point your browser to that script

In this short listing you can see how to create tables with a PHP script :
Code:

// DEFINE parameters for accessing the local MySql Database
/* ------------------------------------------------------------------------------*/
define("DB_SERVER", "localhost"); // in most cases "localhost" will work for you!
define("DB_USER", "root_user"); // replace "root_user" with your username for the MySql database
define("DB_PASS", "database_password"); // replace "database_password" with your password for the MySql database

$con = mysql_connect(DB_SERVER, DB_USER, DB_PASS); // connection to the database
// Table creation
mysql_select_db("YOUR_DATABASE_NAME", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";

mysql_query($sql,$con) ;// Execute the mysql query

mysql_close($con); // Close the connection



Save this listing as 'create_table.php' in C:/Xampp/htdocs/tables and point your browser to
Code:

localhost/tables/create_table.php

A table 'Persons' should be created with 3 fields : 'FirstName', 'LastName' and 'Age'. 2 are of type 'varchar' (character-->string) and 'Age' is int (integer-->number).
To view the content of a table, in PHP we need to build a mysql query :
Code:

<?
$query = "SELECT * FROM Persons ORDER BY Age ASC";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {

echo "First name : ".$row['FirstName']."<br />";
echo "Last name : ".$row['LastName']."<br />";
echo "Age : ".$row['Age']."<br />";
echo "<hr />";
}
?>

This short listing will display all the records in our table Persons!
Inserting, deleting and editing mysql table records are simple and can be done with a PHP script, I will show you how to do that next, you will see a new tag <form> which will 'help us' to manipulate our table records.

Take care! Best regards,

Victor

dsignresponder
Member
Member

Posts: 9
iCoins: 30

View user profile

Back to top Go down

View previous topic View next topic Back to top


Permissions of this forum:
You cannot reply to topics in this forum