cgibie
02-10-2006, 02:21 AM
Hi I tried to create a table called member in my databased called "default" that is provided by my webhost, which means I'm limited to use only that database. I have this code below, but it does not work. Any idea what's wrong with it?
<? php
include 'config.php';
include 'opendb.php';
// create the member table and fields
$query = 'CREATE TABLE member ( '.
// fields
'mid INT NOT NULL AUTO_INCREMENT, '.
'mfname VARCHAR(20) NOT NULL, '.
'mlname VARCHAR(20) NOT NULL, '.
'memail VARCHAR(60) NOT NULL, '.
'PRIMARY KEY(mid)
)';
$result = mysql_query($query);
include 'closedb.php';
?>
cgibie
02-10-2006, 02:24 AM
Yeah I do but I want to try out using sql to create table... apparently I didn't succeed.
cnelson
02-10-2006, 02:32 AM
hmmm, sorry. i'm lazy i guess you could say, and just PHPmyAdmin.
-cnelson. :)
Yakisoba
02-10-2006, 02:34 AM
Try this...
I've included an "if" statment that will tell you if the table has been created or not. In the event there is a problem with your MySQL syntax, it will display the error.
<? php
include 'config.php';
include 'opendb.php';
// create the member table and fields
$query = 'CREATE TABLE member ( '.
// fields
'mid INT NOT NULL AUTO_INCREMENT, '.
'mfname VARCHAR(20) NOT NULL, '.
'mlname VARCHAR(20) NOT NULL, '.
'memail VARCHAR(60) NOT NULL, '.
'PRIMARY KEY(mid)
)';
if( mysql_query( $query ) )
echo( "Table created successfully.<br>" );
else
die( "Error! Could not create table: " . mysql_error() );
As for the Query I would have done this*
$query = "CREATE TABLE member (
mid INT NOT NULL AUTO_INCREMENT,
mfname VARCHAR(20) NOT NULL,
mlname VARCHAR(20) NOT NULL,
memail VARCHAR(60) NOT NULL,
PRIMARY KEY(mid)
)";
if( mysql_query( $query ) )
echo( "Table created successfully.<br>" );
else
die( "Error! Could not create table: " . mysql_error() );
(*note the quotes)
not tested, but it should work.
Hope that helps...
Yakisoba
cgibie
02-10-2006, 02:46 AM
It said nothing... still blank page...
cgibie
02-10-2006, 02:55 AM
problem solved,
Thanks for helping
cgibie
02-10-2006, 02:58 AM
Is there a way to put the error notification code on php? Sometimes the page display without knowing what's going wrong. any idea?
Yakisoba
02-10-2006, 03:02 AM
Not sure what your question is. mysql_error() is a php function.
What did you do to solve your first problem?
Yakisoba
cgibie
02-10-2006, 03:09 AM
Did you notice there is a space after
<? php ... php is so picky, I need to change to <?php (no space)
that's the problem