Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 23-02-2010
Member
 
Join Date: Apr 2009
Posts: 732
How to create MySQL Database and Tables in PHP?

Hi friends,
I have done the programming in PHP to some extent. Now I am working with the database. I am using MySQL for database. I am not having fair knowledge about the using the MySQL in PHP. So I need your help.!! Please tell me how to create MySQL Database and Tables in PHP? Any other information regarding the topic would be grateful.
Reply With Quote
  #2  
Old 23-02-2010
Member
 
Join Date: Jul 2006
Posts: 442
Re: How to create MySQL Database and Tables in PHP?

A database will contain a variable number of tables of data which are arranged in columns and rows. A MySQL table is completely different than the normal coding that you do. You might feel tough to understand but later on you will do it easily. In MySQL and other database systems, the goal is to store information in an orderly fashion. You can do this by using the tables. Also I would like to suggest you that before you can enter data (rows) into a table, you must first define what kinds of data will be stored (columns).
__________________
"When they give you ruled paper, write the other way..." J.R.J.
Reply With Quote
  #3  
Old 23-02-2010
Member
 
Join Date: Mar 2008
Posts: 349
Re: How to create MySQL Database and Tables in PHP?

You can use the following code for creating the table in PHP :
PHP Code:
<?php
mysql_connect
("localhost""admin""1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

mysql_query("CREATE TABLE example(
id INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id),
 name VARCHAR(30), 
 age INT)"
)
 or die(
mysql_error());  

echo 
"Table Created!";

?>
the above code will design a MySQL query to summon the table from database.
Reply With Quote
  #4  
Old 23-02-2010
Member
 
Join Date: Nov 2008
Posts: 996
Re: How to create MySQL Database and Tables in PHP?

Creating the database is also very important. The following example creates a database called my_data :
PHP Code:
<?php
$con 
mysql_connect("localhost","sunil","xyz123");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }

if (
mysql_query("CREATE DATABASE my_data",$con))
  {
  echo 
"Database created";
  }
else
  {
  echo 
"Error creating database: " mysql_error();
  }

mysql_close($con);
?>
Reply With Quote
  #5  
Old 23-02-2010
Member
 
Join Date: Aug 2006
Posts: 227
Re: How to create MySQL Database and Tables in PHP?

You should also know about the primary keys and auto increment fields. You should keep in mind that each table should have a primary key field. To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field. The following code would help you :
Code:
$sql = "CREATE TABLE PersonsDemo 
(
personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
FirstName varchar(15),
LastName varchar(15),
Age int
)";

mysql_query($sql,$con);
__________________
I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to create MySQL Database and Tables in PHP?"
Thread Thread Starter Forum Replies Last Post
How to detect failure of MySQL database tables Agneya Software Development 5 27-02-2010 04:40 AM
How to use Batch Files to Create MySQL Database? DANIEL 602 Software Development 4 05-02-2010 07:03 AM
how to create and restore mysql database using vb 6.0 nanakofiboafo Software Development 1 14-09-2009 09:18 AM
Sample database script to create tables Naresh Modi Software Development 3 12-08-2009 02:17 PM
Mysql select from multiple tables for php. afidelino Software Development 4 09-07-2009 06:13 PM


All times are GMT +5.5. The time now is 09:41 AM.