i'm trying to insert datas in a table of 8 columns but it's doesn't work . i'm using mysql version 5.7.11 . however it works when i reduce it to 6 columns 7 columns refuse to work as well .
please find the tables and their php code below
6 columns mysql/php
1.1 php
$res=$pdo->prepare('INSERT INTO '.$table.'(title,description,image,city,price) VALUES(:titl,:decri,:img,:cty,:prce)');
$res->bindParam(':titl',$title);
$res->bindParam(':decri',$decri);
$res->bindParam(':img',$item_img);
$res->bindParam(':cty',$city);
$res->bindParam(':prce',$price);
1.2 mysql
CREATE TABLE `mobiles`
(`id` int(11) NOT NULL,
`title` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`image` longblob NOT NULL,
`city` varchar(20) NOT NULL,
`price` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
8 columns tables
2.1 mysql code
CREATE TABLE `mobiles` (
`id` int(11) NOT NULL,
`title` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`image` longblob NOT NULL,
`city` varchar(20) NOT NULL,
`price` int(11) NOT NULL,
`user_id` int(10) NOT NULL,
`date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
2.2 php code
$res=$pdo->prepare('INSERT INTO'.$table.'(title,description,image,city,price,user_id,date) VALUES(:titl,:decri,:img,:cty,:prce,:user_id,:dte)');
$res->bindParam(':titl',$title);
$res->bindParam(':decri',$decri);
$res->bindParam(':img',$item_img);
$res->bindParam(':cty',$city);
$res->bindParam(':prce',$price);
$res->bindParam(':user_id',$user_id);
$res->bindParam(':dte',$date);
i have executed right after , the six columns works but the 8 columns one
why is that more than 6 columns mysql can't insert while the number limit of columns can reach 255.
is there any issue in my code ? i echoed all the entries and they all work fine.
any help please ?
You have no space between 'INSERT INTO' and $table in second query, so query fails because of syntax error.
Related
i have this table structure:
CREATE TABLE `schedules_trackings` (
`id` bigint(20) NOT NULL,
`device_id` bigint(20) UNSIGNED NOT NULL,
`schedule_id` bigint(20) UNSIGNED NOT NULL,
`latitude` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`longitude` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` datetime NOT NULL DEFAULT current_timestamp(),
`altitude` double DEFAULT NULL,
`accuracy` double DEFAULT NULL,
`heading` double DEFAULT NULL,
`speed` double DEFAULT NULL,
`activity` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`battery` double DEFAULT NULL,
`segmentid` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`uuid` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`odometer` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE `schedules_trackings`
ADD PRIMARY KEY (`id`),
ADD KEY `device_id` (`device_id`,`schedule_id`,`created_at`) USING BTREE,
ADD KEY `schedule_id` (`id`,`created_at`) USING BTREE;
ALTER TABLE `schedules_trackings`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT;
the mobile device sent me each 10 second the coordinates and the progressive count of the odometer.
the problem i need to remove from this count the distance made with a speed more than 30kmh, i have the speed data but i don't have idea how to remove it from the progressive count.
any solution with mysql query or php?
some example data:
lat1 - lng1 - speed1 - 0mt
lat2 - lng2 - speed2 - 50mt
lat3 - lng3 - speed3 - 100mt
if i only remove for example the row with speed 2 i will get anyway on the last point 100mt but this is not right way. excluding speed 2 my real distance is 50mt this is very simple example, the real query have to work on 25k record avarage.
the target is to get the distance made by walk and discard the distance made by car.
I am using the redbeanPHP ORM and mysql. I have the following table:
CREATE TABLE `mast` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`note` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`geolocation` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`location` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`zip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`state` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`app` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UQ_84a93b55f688c94f73092dba1b9590c41a92cbf5` ('app')
) ENGINE=InnoDB AUTO_INCREMENT=98 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I want to insert records into the 'mast' table providing they are unique with respect to both of the 2 fields listed above. In other words if either 'geolocation' or 'app' is a duplicate, I don't want to insert the associated record.
I am using following php code to create the 2 unique fields:
$mast= R::dispense('mast');
$mast->setMeta("buildcommand.unique" , array(array('geolocation')));
$mast ->import($resultsarray);
$mast->setMeta("buildcommand.unique" , array(array('app')));
$id = R::store($mast); // DUMMY BEAN
A unique index is only being created for the 'app' field . Is there a way to set them both as unique in redbean?
From the documentation it should work like the following:
$mast = R::dispense('mast');
$mast->setMeta("buildcommand.unique.0", array('geolocation', 'app'));
$mast->import($resultsarray);
$id = R::store($mast);
In your code you just overwrote the value for build command.unique.
I'd like to find the number of posts for each user grouped by month.
I'm currently using INT(10) unsigned to store the date of posts.
what would be a super fast way to do this?
CREATE TABLE IF NOT EXISTS `media` (
`pid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`class` tinyint(1) NOT NULL DEFAULT '1',
`date_class_changed` int(10) unsigned NOT NULL,
`title` char(5) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`url` varchar(1024) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`media` enum('image','video') CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`thumb` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`description` varchar(140) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(16) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`date` int(10) unsigned NOT NULL,
`file` varchar(1024) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`hash` char(32) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`hashtag` text CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`meta` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`ip` int(10) unsigned NOT NULL,
`kind` tinyint(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`pid`),
UNIQUE KEY `title` (`title`),
KEY `hash` (`hash`),
KEY `class_date` (`class`,`date_class_changed`),
KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1022724 ;
This is the table, I'm talking about, I'd like to display the number of posts for each user for each mont, such as: september 2012, User X, N posts etc..
The query I'm using after the help from #fthiella is:
SELECT
DATE_FORMAT(FROM_UNIXTIME(`date`), '%Y-%m') as YearMonth, username, COUNT(*) as Posts
FROM
media
WHERE username = 'foobar'
GROUP BY 1
ORDER BY 1 DESC
thanks God it's fast enough, now I'll try to optimize in case it's not using an index, but for now with almost 1M record is doing good. cheers.
SELECT
DATE_FORMAT(FROM_UNIXTIME(`date`), '%Y-%m') as YearMonth,
username,
COUNT(*) as Posts
FROM
media
GROUP BY
DATE_FORMAT(FROM_UNIXTIME(`date`), '%Y-%m') as YearMonth,
username
I'm trying to create a table using PHP, PDO and MySQL.
For the needs of my application, the name of the table has to be a variable.
Here is my code :
$request = $pdo->prepare("CREATE TABLE IF NOT EXISTS :table (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` bigint(20) unsigned NOT NULL,
`position` bigint(20) unsigned NOT NULL,
`left` bigint(20) unsigned NOT NULL,
`right` bigint(20) unsigned NOT NULL,
`level` bigint(20) unsigned NOT NULL,
`title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;");
$request->execute(array(
'table'=>$uuid));
Can't I use ":table" in the MySQL statement ??
Currently I wrote :
[...]
CREATE TABLE IF NOT EXISTS `$uuid`
[...]
This works but it sounds weird to me ^^' Is it the only solution to my problem ?
You can't pass the table name as parameter. If you want to create table with variable name you must use dynamic query.
$pdo->query("CREATE TABLE IF NOT EXISTS userfiles (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int unsigned NOT NULL,
`parent_id` bigint(20) unsigned NOT NULL,
`position` bigint(20) unsigned NOT NULL,
`left` bigint(20) unsigned NOT NULL,
`right` bigint(20) unsigned NOT NULL,
`level` bigint(20) unsigned NOT NULL,
`title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8");
This is THE ONLY proper way of handling such situations.
Such matters are very basic things.
and your current setup is just like a car with square wheels.
Despite of your shortage of time you have to make it single table.
Otherwise you will waste A LOT more time and eventually will turn to the proper design anyway but after innumerable pains
I have a couple of tables in MySQL that I'm working with that do not have any relation. They do have a column of similar data (Postal Codes / Zip Codes).
What I have to do with these tables is compare the postal codes from one table compare them to the postal codes from the first table and count them.
For Example.
Table A has a postal code of T0A and T0B (I use only the first three characters in the postal code as this is all I need to compare against)
Table B has 13 rows where the postal code matches T0A and 3 rows where the postal code matches T0B.
So the outcome would look like:
T0A = 13
T0B = 3
HOWEVER, then I need to take these and separate them by city, so since both T0A and T0B could be one city I would need to take those and add them together and get something like.
Edmonton = 16
I've been doing this with for loops and arrays. So I'm reading the data from table A into one array and the data from table b into another array. Then I compare the postal codes from table B to the postal codes in table A using nested for loops in order to count the number of occurrences of the postal codes and then I store them in another array. This is all fine and dandy however now I'm a bit stuck trying to separate the counts into their correct cities and I'm sitting here thinking there must be an easier way to do this. Does anyone have any suggestions, am I going about this all wrong?
Structure - Table A
jos_postalzip_redirect | CREATE TABLE `jos_postalzip_redirect` (
`id` int(11) NOT NULL auto_increment,
`country_code` varchar(2) NOT NULL,
`prov_state_code` varchar(2) NOT NULL,
`city` varchar(60) NOT NULL,
`postal_zip` varchar(6) NOT NULL,
`email_address` varchar(60) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=739 DEFAULT CHARSET=utf8 |
Structure - Table B
jos_form_submitteddata_form1 | CREATE TABLE `jos_form_submitteddata_form1` (
`id` int(11) NOT NULL auto_increment,
`bf_status` varchar(20) collate utf8_bin NOT NULL,
`bf_user_id` int(11) NOT NULL,
`FIELD_1` varchar(255) collate utf8_bin NOT NULL,
`FIELD_2` varchar(255) collate utf8_bin NOT NULL,
`FIELD_3` varchar(255) collate utf8_bin NOT NULL,
`FIELD_4` varchar(255) collate utf8_bin NOT NULL,
`FIELD_5` varchar(255) collate utf8_bin NOT NULL,
`FIELD_6` varchar(255) collate utf8_bin NOT NULL,
`FIELD_7` varchar(255) collate utf8_bin NOT NULL,
`FIELD_8` varchar(255) collate utf8_bin NOT NULL,
`FIELD_23` varchar(255) collate utf8_bin NOT NULL,
`FIELD_24` varchar(255) collate utf8_bin NOT NULL, //THIS IS THE POSTAL CODE FIELD
`FIELD_28` varchar(255) collate utf8_bin NOT NULL,
`FIELD_29` varchar(255) collate utf8_bin NOT NULL,
`FIELD_30` varchar(255) collate utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4044 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
Just an abstraction of what I understood. You may need to adjust it according to your needs.
In this example I will assume that FIELD_1 in table B is a postal code.
Count by postal code:
select
left(ta.postal_zip, 3) p_code, count(*)
from
jos_form_submitteddata_form1 tb
join jos_postalzip_redirect ta on left(tb.field_1, 3) = left(ta.postal_zip, 3)
group by
p_code
Count by city:
select
ta.city, count(*)
from
jos_form_submitteddata_form1 tb
join jos_postalzip_redirect ta on left(tb.field_1, 3) = left(ta.postal_zip, 3)
group by
ta.city