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?

clydewinux
  • 515
  • 3
  • 9
  • 18
  • 5
    Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://news.php.net/php.internals/53799). See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Instead you should learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) – NullPoiиteя Oct 23 '12 at 07:18
  • and use either [PDO](http://stackoverflow.com/questions/13000289/create-mysql-table-with-php-variable-not-working#13000306) or [MySQLi](http://in3.php.net/mysqli). If you can't decide which, [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you. If you pick PDO, [here is good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). Also see [Why shouldn't I use mysql functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-function-in-php) – NullPoiиteя Oct 23 '12 at 07:19
  • 2
    Also post any error messages, and ensure that your SQL works in MySQL console. – Devraj Oct 23 '12 at 07:19
  • @NullPointer is totally right... First please check that your DB connection is effectively made. – Laurent Brieu Oct 23 '12 at 07:22
  • thank you all for the fast response..ill try using mySQLi.. – clydewinux Oct 23 '12 at 07:22
  • @ClydeWinux try the pdo instead the mysqli its more simple functional – NullPoiиteя Oct 23 '12 at 07:23
  • i have another question.. can i just create batabase through coding the same way as when i create the table? if not is WAMP the only way? – clydewinux Oct 23 '12 at 07:24

2 Answers2

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!"; 
?>
Prem
  • 697
  • 3
  • 10
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!";

SRIRAM
  • 1,888
  • 2
  • 17
  • 17