к примеру
INSERT INTO `images` VALUES ('5', '2012-01-10 14:09:12', 'Второе изображение', '1326197352', NULL, '1');
использую штатную функцию mysql_query()
выдает ошибку
INSERT INTO `images` VALUES ('5', '2012-01-10 14:09:12', 'Второе изображение', '1326197352', NULL, '1');
Цитата |
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO `images` VALUES ('5', '2012-01-10 14:09:12', 'Второе изоб�' at line 2 |
CREATE TABLE IF NOT EXISTS `images` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`description` varchar(500) DEFAULT NULL,
`name` varchar(500) NOT NULL,
`main` int(1) DEFAULT NULL,
`section_id` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;"
Цитата (kent666 @ 24.01.2012 - 16:45) |
Дело в том что в процессе выполнения я не знаю наименование полей!!! |
$db = new db($db_host, $db_user, $db_password, $db_name);
$dump_dir = SVV_ROOT ."setup/dump"; // директория, куда будем сохранять резервную копию БД
if($ok)
{
$res = $db->mysqlQuery("SHOW TABLES");
$sqlFile = SVV_ROOT ."setup/dump/". time() .".sql";
$query = '';
while( $table = mysql_fetch_row($res) )
{
$query .= "TRUNCATE TABLE `". $table[0] ."`;\n";
$r = $db->mysqlQuery('SELECT * FROM `'.$table[0].'`');
if(mysql_num_rows($r) > 0)
{
while ($row = mysql_fetch_assoc($r))
{
$i=0;
$query .= "INSERT INTO `".$table[0]."` VALUES (";
foreach ($row as $field )
{
if (is_null($field)){
$field = "NULL";
}
else{
$field = "'".mysql_escape_string( $field )."'";
}
if($i==0){
$query .= ', ';
}
$query .= $field;
}
$query .= ");\n";
}
}
}
file_put_contents($sqlFile, $query);
reDirect();
}
if($edit)
{
$query = file_get_contents($sqlFile = SVV_ROOT ."setup/dump/1327405437.sql", true);
$db->mysqlQuery($query);
}
Цитата (kent666 @ 24.01.2012 - 16:50) |
Данный скрипт предназначен для резервного копирования базы данных! |
TRUNCATE TABLE `images`;
INSERT INTO `images` (id, date, description, name, main, section_id) VALUES ('7', '2012-01-12 10:33:18', 'Четвертый рисунок', '1326357198', NULL, '1');
INSERT INTO `images` (id, date, description, name, main, section_id) VALUES ('5', '2012-01-10 14:09:12', 'Второе изображение', '1326197352', NULL, '1');
Цитата |
2012-01-24 04:40:43 MySQL error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO `images` (id, date, description, name, main, section_id) VALUES ('7'' at line 2 |
INSERT INTO `images` VALUES (5, '2012-01-10 14:09:12', 'Второе изображение', 1326197352, NULL, 1);
$db = new db($db_host, $db_user, $db_password, $db_name);
$dump_dir = SVV_ROOT ."setup/dump"; // директория, куда будем сохранять резервную копию БД
if($ok)
{
$sqlFile = SVV_ROOT ."setup/dump/". time() .".sql";
$res = $db->mysqlQuery("SHOW TABLES");
$query = array();
while( $table = mysql_fetch_row($res) )
{
$query[]= "TRUNCATE TABLE `". $table[0] ."`";
$res1 = $db->mysqlQuery('SHOW COLUMNS FROM '. $table[0]);
$c = array();
if(mysql_num_rows($res1) > 0)
{
while($row = mysql_fetch_assoc($res1))
{
$c[] = '`'. $row['Field'] .'`';
}
$column = implode(', ', $c);
}
$res2 = $db->mysqlQuery('SELECT * FROM `'.$table[0].'`');
if(mysql_num_rows($res2) > 0)
{
while ($row = mysql_fetch_assoc($res2))
{
$val = array();
foreach ($row as $field )
{
if (is_null($field)){
$field = "NULL";
}else{
$field = "'".mysql_escape_string( $field )."'";
}
$val[] = $field;
}
$query[]= "INSERT INTO `".$table[0]."` (". $column .") VALUES (". implode(', ', $val) .")";
}
}
}
file_put_contents($sqlFile, serialize($query));
reDirect();
}
if($edit)
{
$query = unserialize(file_get_contents($sqlFile = SVV_ROOT ."setup/dump/1327416535.sql"));
foreach($query as $val){
$db->mysqlQuery($val);
}
}