Login | Register | FAQ
Anonymous

Create databases and tables

+ Post a reply

4 posts Page 1 of 1


Create databases and tables

by zjing129 » Sat May 12, 2012 6:34 pm

Assume that there is no database named "mydatabase" in my server. I run the following PHP codes:

Code: Select all
mysql_connect("localhost", "root", "");

mysql_query("CREATE DATABASE mydatabase");

mysql_select_db("mydatabase");

mysql_query("CREATE TABLE people(id INT AUTO-INCREMENT, FirstName CHAR, LastName CHAR, Phone INT, BirthDate DATE, PRIMARY KEY(id))");

$strSQL = "INSERT INTO people(FirstName, LastName, Phone, BirthDate) VALUES('Gus','Goose','99887766','1964-04-20')";

mysql_query($strSQL);

mysql_close();


To see the results, I checked phpMyAdmin. It seems that the database "mydatabase" is created but the table "people" is not. Anyone knows why? Thanks in advance. By the way, instead of the PHP codes, I can create a table named "people" in "mydatabase" using phpMyAdmin.
User avatar

zjing129

  • Posts: 4
  • Joined: Thu May 10, 2012 1:00 am

Re: Create databases and tables

by XainPro » Sun May 13, 2012 9:33 am

1.Yes you can create the table from phpmyadmin
2.Your Query to Create The Table is Not correct just replace the query with the correct syntax written below

Code: Select all
CREATE TABLE people(id INT( 11 ) NOT NULL AUTO_INCREMENT, FirstName CHAR, LastName CHAR, Phone INT, BirthDate DATE, PRIMARY KEY(id))


good Luck.!
User avatar

XainPro

  • Posts: 3567
  • Joined: Fri Feb 17, 2012 8:10 pm

Re: Create databases and tables

by zjing129 » Sun May 13, 2012 8:05 pm

Thanks. I tried your codes. There is still something wrong with the results. I tried to insert a row using the following codes:

$strSQL = "INSERT INTO people(FirstName, LastName, Phone, BirthDate) VALUES('Gus','Goose','99887766','1964-04-20')";

mysql_query($strSQL);


The result for "FirstName LastName" is "G G" instead of "Gus Goose". By the way, is "NOT NULL" mandatory when the table "people" is created?
User avatar

zjing129

  • Posts: 4
  • Joined: Thu May 10, 2012 1:00 am

Re: Create databases and tables

by XainPro » Mon May 14, 2012 5:24 am

Well its not mandatory but its recommended that your primery key should not be null.
and the data insertion query will be check but 1st you sould tell is the table created by php code this time or you have created the table by phpmyadmin and the query to insert data is correct your table structure is not good you have given 1 letter which is CHAR.
You should change the CHAR TO VARCHAR
create table again with this query
Code: Select all
CREATE TABLE people
(id INT( 11 ) NOT NULL AUTO_INCREMENT,
FirstName VARCHAR(200),
LastName VARCHAR(200),
Phone INT,
BirthDate DATE,
PRIMARY KEY(id))


then try to save record it should give complete name now ...
User avatar

XainPro

  • Posts: 3567
  • Joined: Fri Feb 17, 2012 8:10 pm


+ Post a reply

Page 1 of 1

?>