Forum Moderators: open
SELECT lid, cid, sid, title, url, description, date, name, email, hits, submitter, downloadratingsummary, totalvotes, totalcomments, filesize, version, homepage FROM downloads_deluxe
that works and grabs the proper fields/rows...
but then i need it to insert into another table like....
INSERT INTO my_downloads VALUES lid, cid, sid, title, url, description, date, name, email, hits, submitter, downloadratingsummary, totalvotes, totalcomments, filesize, version, homepage
that fails...
i wanna grab info from 17 rows of that table...
and then insert it to another table (a new one)
All fields/rows etc have the same names..
so thats the question realy...
so i thought something like...
$sql = 'SELECT lid , cid , sid , title , url , description , date , name , email , hits , submitter , downloadratingsummary , totalvotes , totalcomments , filesize , version , homepage FROM old_downloads ';
and then somehow then do...
INSERT INTO....etc..
but thats where im stuck..
A field (or column) is one piece of data (e.g., date, name, etc.). A row of data is a collection of data, 1 entry for each field. It's not exactly accurate, but think of a database like an Excel spreadsheet.
Now, once you get the demantics correct, telling us the exact error message(s) you get should bring a solution almost within reach...
What i want is this..
<?php
$db = mysql_connect ("localhost", "username", "password")
or die("no connection: " .mysql_error());
mysql_select_db("my_test", $db);
$sql = "SELECT lid , cid , sid , title , url , description , date , name , email , hits , submitter , downloadratingsummary , totalvotes , totalcomments , filesize , version , homepage FROM old_downloads".
"VALUES ('$lid, $cid, $sid, $title, $url, $description, $date, $name, $email, $hits, $submitter, $downloadratingsummary, $totalvotes, $totalcomments, $filesize, $version, $homepage')";
$sql = "INSERT INTO new_downloads (lid, cid, sid, title, url, description, date, name, email, hits, submitter, downloadratingsummary, totalvotes, totalcomments, filesize, version, homepage)".
"VALUES ('$lid, $cid, $sid, $title, $url, $description, $date, $name, $email, $hits, $submitter, $downloadratingsummary, $totalvotes, $totalcomments, $filesize, $version, $homepage')";
if (!mysql_query($sql)) {
echo "Faild";
} else{
echo "succes";
}
mysql_close($db);
?>
i know i completely fail with this...
the first part does work but inserting the results into is a mess..
i simply get...Faild
I don't know enough PHP or MYSQL. So I don't understand why you have the VALUES part after the select statement (1st $sql).
If the field names and types match exactly, the following should work with MSSQL. But you can try to modify accordingly:
INSERT new_downloads
SELECT *
FROM old_downloads
(MSSQL allows a little sloppiness with INSERTs, so you may need to change the first line to "INSERT INTO new_downloads" to get it to work.)
I still don't understand how the original question about 17 vs. 30 rows fits into any of this.
THIS TABLE AND ITS CONTENTS :
CREATE TABLE `old_downloads` (
`lid` int(11) NOT NULL auto_increment,
`cid` int(11) NOT NULL default '0',
`title` varchar(100) NOT NULL default '',
`url` varchar(255) NOT NULL default '',
`description` text NOT NULL,
`date` datetime NOT NULL default '0000-00-00 00:00:00',
`name` varchar(100) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`hits` int(11) NOT NULL default '0',
`sid` int(11) NOT NULL default '0',
`downloadratingsummary` double(6,4) NOT NULL default '0.0000',
`totalvotes` int(11) NOT NULL default '0',
`totalcomments` int(11) NOT NULL default '0',
`icon` varchar(255) default NULL,
`submitter` varchar(60) NOT NULL default '',
`sub_ip` varchar(16) NOT NULL default '0.0.0.0',
`filesize` bigint(20) NOT NULL default '0',
`version` varchar(20) NOT NULL default '',
`homepage` varchar(255) NOT NULL default '',
`active` tinyint(2) NOT NULL default '1',
`price` decimal(8,2) NOT NULL default '0.00',
`currency` varchar(4) NOT NULL default 'EUR',
`sale` tinyint(2) NOT NULL default '0',
`image` varchar(255) NOT NULL default '',
`share` tinyint(1) NOT NULL default '0',
`previewtrue` tinyint(2) NOT NULL default '1',
`mediacheck` char(3) NOT NULL default 'mp3',
`previewurl` varchar(255) NOT NULL default '',
`presentationurl` varchar(255) NOT NULL default '',
`keywords` text NOT NULL,
PRIMARY KEY (`lid`),
KEY `lid` (`lid`),
KEY `cid` (`cid`),
KEY `title` (`title`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
HAS TO BE REPLACED WITH THIS TABLE :
CREATE TABLE `new_downloads` (
`lid` int(11) NOT NULL auto_increment,
`cid` int(11) NOT NULL default '0',
`sid` int(11) NOT NULL default '0',
`title` varchar(100) NOT NULL default '',
`url` varchar(100) NOT NULL default '',
`description` text NOT NULL,
`date` datetime default NULL,
`name` varchar(100) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`hits` int(11) NOT NULL default '0',
`submitter` varchar(60) NOT NULL default '',
`downloadratingsummary` double(6,4) NOT NULL default '0.0000',
`totalvotes` int(11) NOT NULL default '0',
`totalcomments` int(11) NOT NULL default '0',
`filesize` int(11) NOT NULL default '0',
`version` varchar(10) NOT NULL default '',
`homepage` varchar(200) NOT NULL default '',
PRIMARY KEY (`lid`),
KEY `lid` (`lid`),
KEY `cid` (`cid`),
KEY `sid` (`sid`),
KEY `title` (`title`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
So you see...
The first table is BIGGER...
But im not gonna use these anymore..!
`icon` varchar(255) default NULL,
`sub_ip` varchar(16) NOT NULL default '0.0.0.0',
`active` tinyint(2) NOT NULL default '1',
`price` decimal(8,2) NOT NULL default '0.00',
`currency` varchar(4) NOT NULL default 'EUR',
`sale` tinyint(2) NOT NULL default '0',
`image` varchar(255) NOT NULL default '',
`share` tinyint(1) NOT NULL default '0',
`previewtrue` tinyint(2) NOT NULL default '1',
`mediacheck` char(3) NOT NULL default 'mp3',
`previewurl` varchar(255) NOT NULL default '',
`presentationurl` varchar(255) NOT NULL default '',
`keywords` text NOT NULL,
Now...i cant be clearer then this.. :(
However, your new table has smaller sizes for some of the fields (example- url is VARCHAR(255) in old_downloads, but only 100 in new_downloads), so you'll need to trim some of the data. I don't know what you use in PHP/MYSQL- "substr" maybe?
For future reference, if you had originally asked how to copy data from a table with 30 fields into a table with 17 fields, the solution would have arrived much sooner. You may want to check out this link: [catb.org...]
[edited by: LifeinAsia at 12:06 am (utc) on Aug. 11, 2006]
thanks...im deffinately gonna try this after some sleep so im back on this in about 7 hours or so..
as you said........I don't know what you use in PHP/MYSQL- "substr" maybe?
what you mean by that...
keep in mind that my knoweledge is limited... :)
but as soon as i tried your sample ill get back..
thanks so far :)