Jump to content

Building MySQL tables.


Ruud Hermans

Recommended Posts

Could some one tell me if this is done right? I've written it with the help of some online tutorials.

<?phpinclude("config.php");mysql_connect(localhost,$user,$password);@mysql_select_db($database) or die( "Unable to select database");$query1="CREATE TABLE firearms (name varchar(255) primary key,caliber varchar(255),trigger_action varchar(255),magazine_capacity varchar(255),lock varchar(255),weight varchar(255),lenght varchar(255),height varchar(255),barrel_lenght varchar(255),triggerstop varchar(255),scope varchar(255),scope_lenght varchar(255),comments text)";$query2="CREATE TABLE user (email varchar(255) primary key,name varchar(255),password varchar(255))";mysql_query($query1, $query2);mysql_close();?>

Ruud.

Link to comment
Share on other sites

I am kind of new to php but idk if this right or not it different from my register thingy...@mysql_select_db($database) or die( "Unable to select database");why is there a @ ?this what i find in w3school

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con)  {  die('Could not connect: ' . mysql_error());  }// Create databaseif (mysql_query("CREATE DATABASE my_db",$con))  {  echo "Database created";  }else  {  echo "Error creating database: " . mysql_error();  }// Create table in my_db databasemysql_select_db("my_db", $con);$sql = "CREATE TABLE person (FirstName varchar(15),LastName varchar(15),Age int)";mysql_query($sql,$con);mysql_close($con);?>

and i find out one of the table field thing comments text it don't have varchar or anything...

Link to comment
Share on other sites

i also don't see the need for the @ symbol .. never heard of using that .. try this code

<?php$con = mysql_connect("insert_hostname_here","insert_username_here","insert_password_here");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("insert_db_name_here", $con);$query1="CREATE TABLE firearms (name varchar(255) primary key,caliber varchar(255),trigger_action varchar(255),magazine_capacity varchar(255),locktype varchar(255),weight varchar(255),lenght varchar(255),height varchar(255),barrel_lenght varchar(255),triggerstop varchar(255),scope varchar(255),scope_lenght varchar(255),comments text)";$query2="CREATE TABLE user (email varchar(255) primary key,name varchar(255),password varchar(255))";mysql_query($query1, $con);mysql_query($query2, $con);mysql_close($con);?>

insert your database name, password, username, and hostname where i have put the arbitray place holders. the second parameter in the mysql_query is the connection variable. let me know if this works. i tested it and it works fine for me .. note that i changed one of your fields in the firearms table from lock to locktype .. when i used the word lock it didn't work .. but when i changed it .. it worked fine .. maybe that word is a keyword for databases .. not sure .. hope this helps

Link to comment
Share on other sites

The @ symbol is used to prevent error messages from appearing if there happens to be an error while executing the function.The problem with your code is that you can only perform one query at a time. This won't work:

mysql_query($query1, $query2);

Just use one parameter and run the function twice:

mysql_query($query1);mysql_query($query2);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...