|
PHP Database Integration With MySQL |
|
|
|
|
The Article -
php
|
|
Written by Chetankumar Akarte
|
|
Wednesday, 07 March 2007 |
|
Page 2 of 7
Create new Database :In the next example, we are creating a database user and under that, we are creating a table visitor. The table visitor will be created with four columns: a primary key called ‘id’ that will be auto incremented as data is added to the table, and the remaining three columns are the character (VARCHAR) fields: ‘name’, ‘address’ and ‘email’. Code-2 will help you to create the table successfully.
Code 2 :- db.php
<html>
<head>
<title>Create Database </title>
</head>
<body>
<p>Creating Database & table in MySQL.</p>
<?php
// Connecting to MySQL
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password") or die("Could not connect : " . mysql_error());
print "Connected successfully<P>";
$DB = "user";
$table = "visitor";
$query = "CREATE DATABASE $DB";
$result = mysql_query($query) or die("ERROR while creating database".mysql_error());
print("OK, database made, name of DB : $DB<br><br>");
mysql_select_db($DB, $link);
$query2 = "CREATE TABLE $table (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(25), address varchar(50), email varchar(25))";
$result2 = mysql_query($query2) or die("ERROR while creating table".mysql_error());
print("OK, table made, name of table : visitor<br><br>");
?>
</body>
</html>
If db.php runs without any error, it generates the output as shown in Output-2.
Output 2: db.php
Testing working of MySQL.
Connected successfully
OK, database made, name of DB: user
OK, table made, name of table : visitor
|
|
Last Updated ( Tuesday, 13 March 2007 )
|