Current location: Hot Scripts Forums » Programming Languages » PHP » Error when trying to create MySQL table via PHP


Error when trying to create MySQL table via PHP

Reply
  #1 (permalink)  
Old 01-06-04, 04:48 PM
HasansWeb HasansWeb is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Error when trying to create MySQL table via PHP

Okay, I've been studying PHP for 5 days now & MySQL for about 2 using a book I decided to pick-up. I thought maybe I should try and see what I've learned so far so I decided to write a simple script for posting and grabbing news on my website.

I can't seem to create this table in PHP.

__________________________________________________ ___

<?php

$page_title = 'Add News Table';

if (isset($_POST['submit']))

{
$messege = NULL;
require_once("mysql_connect.php");
$query = "CREATE TABLE news {
id int(10) NOT NULL auto_increment,
username char(16) NOT NULL,
password char(16) NOT NULL,
title varchar(100) NOT NULL,
text text NOT NULL,
date DATETIME NOT NULL,
PRIMARY KEY (id)
}";

$result = mysql_query ($query);

if ($result == true) {

header ('Location: setuser.php');

exit();

} else {

$messege = "It didn't work, sorry!";

}


} // END OF MAIN SUBMIT CONDITIONAL

?>
__________________________________________________ ___

I keep getting the messege I set with the (mysql query result is true) conditional to see if the table and rows were created. I would appreciate it if someone could tell me what I'm doing wrong and how to fix the problem.

Many thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 01-06-04, 05:17 PM
HasansWeb HasansWeb is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
I just remembered that you can use mysql_error() to get the exact error. Here's what it says:

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 '{ id int(10) NOT NULL auto_increment, username char(16)

Alright, and I'm running MySQL 4.0.15-standard.

Please help!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 01-06-04, 06:13 PM
musicalmidget's Avatar
musicalmidget musicalmidget is offline
Newbie Coder
 
Join Date: Jun 2003
Location: United Kingdom
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
It's probably the {} marks in your query which are causing the problems. Try making this your query instead...

PHP Code:

$query "CREATE  TABLE news (

id int( 10  )  NOT  NULL  AUTO_INCREMENT ,
username char( 16  )  NOT  NULL , 
PASSWORD char( 16  )  NOT  NULL ,
title varchar( 100  )  NOT  NULL ,
text text NOT  NULL ,
date DATETIME NOT  NULL , 
PRIMARY  KEY ( id ))"

__________________
Has anyone seen my signature?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-16-11, 09:23 PM
faisals999 faisals999 is offline
Newbie Coder
 
Join Date: May 2011
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
the code ypu have mentioned earlier to test errors is giving following error

Quote:
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 '-345 (`name` VARCHAR(30) NOT NULL, `num_pass` INT(2) NOT NULL, `address` VARCHAR' at line 1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 05-17-11, 11:29 AM
kfurlong's Avatar
kfurlong kfurlong is offline
Wannabe Coder
 
Join Date: Oct 2010
Posts: 148
Thanks: 6
Thanked 20 Times in 20 Posts
Quote:
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 '-345 (`name` VARCHAR(30) NOT NULL, `num_pass` INT(2) NOT NULL, `address` VARCHAR' at line 1
Can you post the new query, I dont see a name varchar 30 anwhere...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 05-19-11, 07:59 AM
Rog3r Rog3r is offline
Newbie Coder
 
Join Date: May 2011
Posts: 6
Thanks: 0
Thanked 1 Time in 1 Post
Quote:
... the right syntax to use near '-345 (`name` VARCHAR...
It seems the table name is no longer "news", but something ending with "-345". When MySQL table names contains special characters (like the dash) then you need to enclose the table name in backticks: CREATE TABLE `tablename-345` (`name` VARCHAR...

Last edited by Rog3r; 05-19-11 at 08:01 AM. Reason: spelling error
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 03:22 AM
mysql table wcfoone PHP 0 12-18-03 01:31 PM
Declared Functions skipper23 PHP 4 12-17-03 11:06 AM
index page not showing up skipper23 PHP 3 12-15-03 02:10 PM
MySQL with PHP question. HELP for a newbie kenfused PHP 3 08-02-03 01:53 AM


All times are GMT -5. The time now is 01:20 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.