[SOLVED] How to create Mysql table using PHP script?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
sisleysusie
Forum Newbie
Posts: 16
Joined: Wed Jan 14, 2004 9:44 pm

[SOLVED] How to create Mysql table using PHP script?

Post by sisleysusie »

Hi all, i am very new in using PHP and MySQL. I am wondering whether i can create MySQL table using PHP script since there is no mysql_query that i can find to create tables using PHP script. Thanks in advance.
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Query this.

Code: Select all

create table table1
(name varchar(60),
firstname varchar(15))
sisleysusie
Forum Newbie
Posts: 16
Joined: Wed Jan 14, 2004 9:44 pm

Post by sisleysusie »

Let's say i am using coding like below:


$query = "CREATE table email ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), email VARCHAR (70))";
mysql_query($sql,$link) or die (mysql_error());

I am creating one table email. Can i create another table in the same $query? Thanks
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Just add a semi-colon after your first create table statement and write another one.

$query = "CREATE table email ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), email VARCHAR (70)); CREATE table email2 ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), email VARCHAR (70));";
mysql_query($sql,$link) or die (mysql_error());
sisleysusie
Forum Newbie
Posts: 16
Joined: Wed Jan 14, 2004 9:44 pm

Post by sisleysusie »

Thanks for the replies. How can i know that the tables have been created? I tried to check by using this code


$query = "SELECT id FROM individual_general_info WHERE id=1";
$result = mysql_query($query);
if(!empty ($result["id"]))
{
echo $result["id"]; }
else
echo "Table is exist but contains no data";

Can i know whether the table is exist by using that code? Thank you
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

You can list all tables by using the command mysql_list_tables().

See this link for more info.

http://ca3.php.net/manual/en/function.m ... tables.php
sisleysusie
Forum Newbie
Posts: 16
Joined: Wed Jan 14, 2004 9:44 pm

Post by sisleysusie »

Thanks microthick. I really appreciate your help
Post Reply