Search code examples
phpmysqlcreate-table

PHP script to create table


I need to create MySQL table. So I'm running this script but it's just not working. Any idea why?

<?php
$con = mysql_connect("database.dcs.aber.ac.uk","xxx","nnnnnnnnnn");
mysql_select_db("jaz",$con);
$sql = "CREATE TABLE storys
(
id int NOT NULL AUTO_INCREMET,
title TINYTEXT,
type TINYTEXT,
link TEXT,
preview TINYTEXT,
tags TINYTEXT,
text MEDIUMTEXT,
updated TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP,
created DATETIME() DEFAULT NULL,
PRIMARY KEY(id)
)";

mysql_query($sql,$con);

mysql_close($con);

?>

Solution

  • Your code has absolute NO error handling, which would have shown you the reason the query's failing.

    $con = mysql_connect(...) or die(mysql_error());
    
    $res = mysql_query(...) or die(mysql_error());
    

    is the bare minimum error handling you should have on pretty much every mysql call.

    Had this been in place, you'd have to been told:

    id int NOT NULL AUTO_INCREMET,
                                ^---missing 'n', not a valid SQL keyword.