Connect with a Database
Page 1 of 1 • Share •
Connect with a Database
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'!
Now point your browser to
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 :
Save this listing as 'create_table.php' in C:/Xampp/htdocs/tables and point your browser to
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 :
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
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'!
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

- Posts: 9
iCoins: 30
Permissions of this forum:
You cannot reply to topics in this forum










