1

i am new to php and mysql. i am now having online tutorials for this topic. i use wamp and already created various databases without tables in it yet. now i created a file createTable.php with these line of codes:

<?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!";
?>

in this code i create a table named example. now when i check inside phpmyAdmin in wamp the example table is not added on the test database. what's the problem with my code? or is it the WAMP?

7
  • 5
    Please, don't use mysql_* functions to write new code. They are no longer maintained and the community has begun deprecation process. See the red box? Instead you should learn about prepared statements Commented Oct 23, 2012 at 7:18
  • and use either PDO or MySQLi. If you can't decide which, this article will help you. If you pick PDO, here is good tutorial. Also see Why shouldn't I use mysql functions in PHP? Commented Oct 23, 2012 at 7:19
  • 2
    Also post any error messages, and ensure that your SQL works in MySQL console. Commented Oct 23, 2012 at 7:19
  • @NullPointer is totally right... First please check that your DB connection is effectively made. Commented Oct 23, 2012 at 7:22
  • thank you all for the fast response..ill try using mySQLi.. Commented Oct 23, 2012 at 7:22

2 Answers 2

1

I tried your code and see its running, just make some changes it it. while you using wamp then try this code.

<?php 
    mysql_connect("localhost", "root", ""); 
    mysql_select_db("test"); 
    mysql_query("CREATE TABLE example( id INT NOT NULL AUTO_INCREMENT,  PRIMARY KEY(id), name VARCHAR(30),  age INT)");    
    echo "Table Created!"; 
?>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks so much this code works.. :-) should i use mysql or mysqli?
0
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, 
'name'  VARCHAR(30), 
'age'   INT,
 PRIMARY KEY('id'))
  ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;")
 or die(mysql_error());  

echo "Table Created!";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.