0

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);

?>
7
  • does the user have write access to the table in question Commented Feb 22, 2012 at 15:56
  • and are the connectionparameters correct? Commented Feb 22, 2012 at 15:57
  • add the line echo mysql_error(); below the line mysql_query($sql, $con); and then tel me if it outputs anything, this line usually tells you what error is occuring and most likely the reason for your problem Commented Feb 22, 2012 at 15:57
  • this is error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ') ON UPDATE CURRENT_TIMESTAMP, created DATETIME() DEFAULT NULL, Commented Feb 22, 2012 at 16:00
  • What are these lines: updated TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP, created DATETIME() DEFAULT NULL, ment to do? are you wanting a date field? Commented Feb 22, 2012 at 16:05

2 Answers 2

4

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.
Sign up to request clarification or add additional context in comments.

Comments

0

Among the previously listed issues, you are using functions in the data type definitions, and the "ON UPDATE" syntax is wrong.

Here is what I think you are looking for in the SQL:

CREATE TABLE IF NOT EXISTS  `storys` (
 `id` INT NOT NULL AUTO_INCREMENT ,
 `title` TINYTEXT,
 `type` TINYTEXT,
 `link` TEXT,
 `preview` TINYTEXT,
 `tags` TINYTEXT,
 `text` MEDIUMTEXT,
 `updated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
 `created` DATETIME DEFAULT NULL ,
PRIMARY KEY ( id )
);

4 Comments

What version of MySQL are you using, and can you give the error message again, so we can be sure it is exactly the same?
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP , created DATE
Can you provide the "new" SQL you're running?
And perhaps this has something to do with it: stackoverflow.com/a/4897134/682232

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.