putting comments in the middle of a createtable statement - php

I got this create table statement;
$sql = "CREATE TABLE TermsFinal(
`seed` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`source` varchar(100),
`old_term` varchar(255),
`new_term` varchar(100),
`same_as` varchar(100),
`last_access` datetime)";
Is there a way to put comments into this statement to the same effect as follows?
$sql = "CREATE TABLE TermsFinal(
`seed` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`source` varchar(100), //sample value services.media
`old_term` varchar(255), // something like "services.media>category:perspective"
`new_term` varchar(100), // something like "opinion"
`same_as` varchar(100), // the seed id of another record or another "old_term" from this table
`last_update` datetime)"; // when the last update took place

Try following SQL comment syntax, but be careful with " in your text
$sql = "CREATE TABLE TermsFinal(
`seed` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`source` varchar(100), -- sample value services.media
`old_term` varchar(255), -- something like "services.media>category:perspective"
`new_term` varchar(100), -- something like "opinion"
`same_as` varchar(100), -- the seed id of another record or another "old_term" from this table
`last_update` datetime)"; // when the last update took place
Read more...

you would have to use sql comments in the lines your still in the sql statement. for mysql this would be:
$sql = "CREATE TABLE TermsFinal(
`seed` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`source` varchar(100), /* sample value services.media */
`old_term` varchar(255), /* something like "services.media>category:perspective" */
`new_term` varchar(100), /* something like "opinion" */
`same_as` varchar(100), /* the seed id of another record or another "old_term" from this table */
`last_update` datetime)"; // when the last update took place

Related

I can't drop a table or create a Mysql table

I am creating a MySQL database and this doesn't seem to work, i was able to create a review table but now i'm trying to drop that table and create a reviews table but it doesn't seem to work. Please can someone take a look at this and help me check to see what's wrong here?
$reviewsTable = "CREATE TABLE reviews (
ID int NOT NULL AUTO_INCREMENT,
Name varchar(100) NOT NULL,
Website varchar(100) NOT NULL,
Review varchar(100) NOT NULL,
TimeOfYear varchar(50),
DayOfYear varchar(50),
PRIMARY KEY (website)
)";
$drop = "DROP TABLE review";
mysqli_query($connect,$drop);
mysqli_query($connect,$reviewsTable);
Just use if exists to drop the table if there is one then create your table.
Id has to be primary key because of the auto increment. all auto increments have to be primary key. You can index website though. but i set id as primary key below this should help.
$reviewsTable = "
DROP TABLE IF EXISTS review;
CREATE TABLE reviews (
ID int NOT NULL AUTO_INCREMENT,
Name varchar(100) NOT NULL,
Website varchar(100) NOT NULL,
Review varchar(100) NOT NULL,
TimeOfYear varchar(50),
DayOfYear varchar(50),
PRIMARY KEY (ID)
)";

i want to create table with php variable + name

i want to create table with php variable+ name
like.
here is my code
if($conn->query($sql1)===TRUE){
$_SESSION['username'] = $x;
$sql = "CREATE TABLE $x
(
ID int NOT NULL AUTO_INCREMENT,
image blob,
date datetime,
status longtext,
PRIMARY KEY (ID)
)";
$sql2 = "CREATE TABLE ".$x."_frnd
(
ID int NOT NULL AUTO_INCREMENT,
myfrndname varchar(255),
myfrndusername varchar(255),
PRIMARY KEY (ID)
)";
$result= mysqli_query($conn,$sql,$sql2);
if ($conn->query($result) === TRUE) {
}
here ($x) is a variable and frnd is mannual name-
i want result table like this.
[ adminfrnd ].
please suggest me.
Try something like this:
CREATE TABLE IF NOT EXISTS `{$x}frnd` (
`id` mediumint(6) unsigned zerofill NOT NULL auto_increment,
`myfrndname` varchar(255) NOT NULL,
`myfrndlastname` varchar(255) NOT NULL,
`myfrndusername` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
by the way
Why do you want to make own table for each admin ?
What you have to do is to append text like this:
$sql = "CREATE TABLE " . $x . "frnd
( ID int NOT NULL AUTO_INCREMENT,
myfrndname varchar(255),
myfrndlastname varchar(255),
myfrndusername varchar(255),
PRIMARY KEY (ID) )";
In your example $x should be string.
Your code will look like:
$x="admin";
$sql = "CREATE TABLE ".$x."_frnd
( ID int NOT NULL AUTO_INCREMENT,
myfrndname varchar(255),
myfrndlastname varchar(255),
myfrndusername varchar(255),
PRIMARY KEY (ID) )";
$result= mysqli_query($conn,$sql);
if ($conn->query($result) === TRUE) {
}
Note: its never good habit to use .(dot) in table name use _(underscore) instead

Why doesn't the primary key field exist in a MySQL database when creating a table?

The exact error I keep seeing is:
Key column 'alarmID' doesn't exist in table
alarmID is my primary key field.
Here is the code I have:
$sql = "CREATE TABLE IF NOT EXISTS alarms (
alaramID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (alarmID),
Title CHAR(30),
Description TEXT,
DT DATETIME
)";
Note: I am coding in PHP.
$sql = "CREATE TABLE IF NOT EXISTS alarms (
alaramID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (alaramID),
Title CHAR(30),
Description TEXT,
DT DATETIME
)";
alaramID
The primary key in your table is alaramID and note the error its alarmID.So correct the spelling in the query like this
$sql = "CREATE TABLE IF NOT EXISTS alarms (
alaramID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (alaramID),
Title CHAR(30),
Description TEXT,
DT DATETIME
)";

MySQLi CREATE Table query not working

$sql = "CREATE TABLE comments
(
ID INT NOT NULL AUTO_INCREMENT,
PosterName VARCHAR(32),
Title VARCHAR(32),
Content VARCHAR(500)
)";
$con->query($sql);
No errors, connection to database is successful. Does anyone know why it doesnt work?
You should have seen that error with that statement:
Incorrect table definition; there can be only one auto column and it must be defined as a key:
auto_increment column must have an UNIQUEindex on them, or more generally being the PRIMARY KEY:
$sql = "CREATE TABLE comments
(
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
PosterName VARCHAR(32),
Title VARCHAR(32),
Content VARCHAR(500)
)";

mySQL DB: Making simultaneous entries?

Consider the following tables for a LMS:
Item (
ID BIGINT NOT NULL UNIQUE AUTO_INCREMENT,
ConnectLog BIGINT NOT NULL,
ItemClass BIGINT NOT NULL,
ItemType BIGINT NOT NULL,
AcqDate TIMESTAMP NOT NULL DEFAULT NOW(),
Vendor BIGINT NOT NULL,
Cost DECIMAL(64,2) NOT NULL DEFAULT '0.00',
Image VARCHAR(255),
Access INTEGER NOT NULL,
Notes VARCHAR(255),
PRIMARY KEY (ID)
)
Book (
ID BIGINT NOT NULL UNIQUE AUTO_INCREMENT,
Item BIGINT NOT NULL UNIQUE,
ISBN BIGINT,
Title VARCHAR(255) NOT NULL,
Authors VARCHAR(255),
Publisher VARCHAR(255),
DDC VARCHAR(255),
PubDate DATETIME,
Edition VARCHAR(255),
BookCase VARCHAR(255),
Shelf VARCHAR(255),
PRIMARY KEY (ID)
)
Now when a user makes an entry for Book, first an entry for Item has to be created first. But i need to find the ID for the Item entry that was created so i can use that value for Item in the Book table...
How? :/
Use mysql_insert_id()
// Create Entry
$sql = "INSERT INTO TABLE () VALUES()";
mysql_query($sql);
$id = mysql_insert_id();
// Create Book
$sql = "INSERT INTO TABLE (`Item_ID`) VALUES(".$id.")";
mysql_query($sql);
I bet there is a MySQL Command you could use to do it in a single query. But, this works.
According to this link, you could do an SQL query like:
INSERT INTO foo (auto,text) VALUES(NULL,'text'); # generate ID by inserting NULL
INSERT INTO foo2 (id,text) VALUES(LAST_INSERT_ID(),'text'); # use ID in second table
I haven't tested it, but it seems as though LAST_INSERT_ID() contains the last inserted ID, no matter what table it was inserted into.
If you're worried about heavy loads, and LAST_INSERT_ID() not containing the appropriate entry's ID, you could wrap these SQL statements in a transaction.

Categories