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);
?>
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.
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 )
);
Related
CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`form_id` tinyint(4) NOT NULL COMMENT '1=shortfor;2=longfom',
`remote_addr` varchar(19) NOT NULL,
`type` tinyint(4) NOT NULL COMMENT '1=impression;2=click;3=conversion',
`website_url` varchar(50) NOT NULL,
`c_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `form_id` (`form_id` , `remote_addr` , `type` , `website_url`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=64;
i want to total count impression,click,conversion.
i want group by website url group by impression,group by click ,group by conversion.
how to wright query? please help me i need to emergency !!!!!
![enter image description here][1]
I need following result
website url----------Impression----click--converison-----
192.1.1.1--------------1-----------2------1--------------
192.1.1.2--------------3-----------2------2--------------
192.1.1.3--------------4-----------3------1--------------
192.1.1.4--------------2-----------6------1--------------
please try below query
$sql = "SELECT website_url,SUM(type=1) as impression,SUM(type=2) as click,SUM(type=3) as conversion FROM `test` GROUP BY website_url";
Let me know this is the way you need.
I am trying to make a revisions page, but when I insert a new revision, it sets the id to 0 instead of 1 more than the last entry, so I can make 1 new revision, but no more. Here is my mySQL code:
("INSERT INTO `revisions` (version, author, content, `date`) VALUES ('".$version."', '".$_SESSION['username']."', '".$content."', '".$date."')");
Edit:
Wow that was embarrassing... I made the table id default 0...
Schema:
CREATE TABLE `revisions` (
`id` int(11) NOT NULL,
`version` varchar(255) NOT NULL,
`author` varchar(255) NOT NULL,
`content` varchar(255) NOT NULL,
`status` int(255) NOT NULL,
`date` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
:) it saids clearly
id int(11) NOT NULL DEFAULT '0'
it will be inserted as default value 0 if you dont make any value , then it will be 0 as default.
if you wanna be 1 as default then use this
id int(11) NOT NULL DEFAULT '1'
or if you want it be incremented then use this
id int(11) NOT NULL AUTO_INCREMENT
//this will be automatically 1,2,3,4,...values incremented everytime you inseret new value
CREATE TABLE `revisions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
......
you need to set id with auto increment
check out the mysql documentation: http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
You should be adding AUTO_INCREMENT to your id instead. That way, it'll just increment automatically (as the name suggests) each time you insert a row.
CREATE TABLE `revisions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
...
<?php
mysql_query("CREATE TABLE IF NOT EXISTS test.$p (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
`colum` varchar(255) NOT NULL DEFAULT '',
`ord` varchar(255) NOT NULL DEFAULT '',
`tex` varchar(255) NOT NULL DEFAULT '',
`search` varchar(255) NOT NULL DEFAULT '',
`count` varchar(255) NOT NULL DEFAULT '',
`order` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
INSERT INTO $p ( `title`, `colum`, `ord`, `tex`, `search`, `count`, `order`) VALUES
('$a', '$b', '$c', '$d', '$f', '$h', '$g'); ");
?>
I am working in a PHP language . $r is my database and $p is my table name
In this I am creating a table , if table is not created and if the table is created then i want to insert the values in the respective column given above but I am not good at mysql_query so I don't know where to add the insert query
I found a solution for my problem but this code is properly working in the phpmyadmin but when i run this code using php , it show me nothing inthe database
You can not execute two queries with a single mysql_query().
Make another call to mysql_query() with the INSERT query as the parameter.
If you absolutely must execute multiple queries in a single function call, change your mysql engne to mysqli, then use mysqli_multi_query() like so:
mysqli_multi_query ($link, 'query1;query2;query3;...');
Please keep in mind that although both approaches issue queries sequentially, their execution is not atomic. If you need atomicity, use a TRANSACTION.
The 13.1.17. CREATE TABLE Syntax can do something like:
CREATE TABLE IF NOT EXISTS `table1` (
`col1` INT (11) DEFAULT NULL,
`col2` INT (11) DEFAULT NULL
)
SELECT 1 `col1`, 2 `col2`;
and should work with mysql_query
I'm having trouble inserting image data into my database. I have a table called images. When dumped with PHPMyAdmin it looks like this:
CREATE TABLE IF NOT EXISTS `images` (
`id` int(11) NOT NULL,
`orig_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`hash` varchar(6) COLLATE utf8_unicode_ci NOT NULL,
`filename` varchar(12) COLLATE utf8_unicode_ci NOT NULL,
`uploaded` datetime NOT NULL,
`views` int(11) NOT NULL DEFAULT '0',
`album_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`server_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `server_id` (`server_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
This is the code I'm using to insert rows:
// Database connection
$db = new PDO('mysql:host=localhost;dbname=testdb', 'root', '');
// some code...
$st = $db->prepare('INSERT INTO `images` (orig_name, hash, filename, uploaded, server_id)
VALUES (?, ?, ?, ?, (SELECT `id` FROM `servers` WHERE `name` = ?))');
$st->execute(array($origName, $fileHash, $filename, date('c'), $server));
// more code...
// Database cleanup
$st = null;
$db = null;
The script returns no errors, and works flawlessly for the first row inserted. If the script runs again, it fails to insert any more rows in the images table. I see no reason why it'd behave like this, the data going into each field is unique each time (except for the server_id field).
Your id field isn't set to auto_increment.
The first record that you post will be added, with a NULL as id; the second record won't be added because there's already a record with NULL as the primary key, so it'll fail - you don't have any error checking in the code, so it won't be printing out the errors it's getting back.
I'm integrating an RSS feed unto my website and i need to create a table:
CREATE TABLE `stories` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
`description` text NOT NULL,
`when` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
)
i know how to insert id, title, description but i do not know how to insert the time of the post (user submits them). I used the script given here.
mysql_query("insert into stories (title,description) VALUES ('$_POST[title]','$_POST[description]') ");
For when use the date function:
date('Y-m-d h:i:s')
By default the date function uses the current date/time if you don't supply one as the second argument.