Find posts of user grouped by month, unixtime - php

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

Related

How to handle increasing data over time in MySQL and PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a list of contents on my website and it is getting filled with records rapidly, for example after one month, there are 300K records in it. Because of this, the list page is responding slower and slower over time.
This page has this functionalities:
Searching
Paging
Also this table Left Joins other tables and it increases the run-time.
I run two queries every time this page is loaded, one for getting 10 limited records, and the other for getting number of all records.
How do I handle this amount of data with out jeopardizing user experience?
EDIT
Here is my Query:
SELECT *,
`note`.`attached_file` AS `attached_file`,
`note`.`description` AS `description`,
`note`.`id` AS `id`,
`note_type`.`title` AS `title`,
`note_goal`.`title` AS `goal`
FROM `note`
LEFT JOIN `contact` ON `note`.`id_contact`=`contact`.`id`
LEFT JOIN `contact_activity_field` ON `contact_activity_field`.`id_contact`=`contact`.`id`
LEFT JOIN `activity` ON `contact_activity_field`.`id_activity`=`activity`.`id`
LEFT JOIN `note_type` ON `note`.`title`=`note_type`.`id`
LEFT JOIN `note_goal` ON `note`.`goal`=`note_goal`.`id`
WHERE
( `note_type`.`title` LIKE '%$q%' OR
`firstname_eng` LIKE '%$q%' OR
`lastname_eng` LIKE '%$q%' OR
`firstname_per` LIKE '%$q%' OR
`lastname_per` LIKE '%$q%' OR
`company_name` LIKE '%$q%' OR
`company_name_per` LIKE '%$q%' OR
`description` LIKE '%$q%' OR
`note_goal`.`title` LIKE '%$q%' ) AND some other condition
GROUP BY `note`.`id`
ORDER BY `note`.`id` DESC
LIMIT $start_from, 10
EDIT 2
note table
CREATE TABLE `note` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`id_user` int(10) NOT NULL,
`id_contact` int(10) NOT NULL,
`title` int(10) DEFAULT NULL,
`goal` int(10) DEFAULT NULL,
`register_date` date DEFAULT NULL,
`description` text COLLATE utf8_unicode_ci,
`attached_file` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL,
`meeting_place` text COLLATE utf8_unicode_ci,
`start_time` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`finish_time` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3297 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
contact table
CREATE TABLE `contact` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_user` int(10) NOT NULL DEFAULT '0',
`id_user_registered` int(10) DEFAULT NULL,
`code` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`contact_type` varchar(20) COLLATE utf8_unicode_ci DEFAULT 'legal',
`firstname_eng` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`lastname_eng` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`firstname_per` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`lastname_per` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`gender` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`id_number` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`national_code` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`birth_date` date DEFAULT NULL,
`company_name` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL,
`company_name_per` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`recommender_eng` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`recommender_per` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`company_type` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`registration_type` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`registration_date` date DEFAULT '1900-01-01',
`registration_number` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`national_id` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`economic_code` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`website` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`postal_code` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`address_eng` text COLLATE utf8_unicode_ci,
`address_per` text COLLATE utf8_unicode_ci,
`phone_number` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`email` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`fax` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`activity_comment` text COLLATE utf8_unicode_ci,
`level` varchar(50) COLLATE utf8_unicode_ci DEFAULT 'Basic',
`guild` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL,
`verify_comment` text COLLATE utf8_unicode_ci,
`status` int(1) NOT NULL DEFAULT '0',
`comment` text COLLATE utf8_unicode_ci,
`submitted` int(1) NOT NULL DEFAULT '0',
`assign_date` datetime DEFAULT NULL,
`created_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_date` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=57357 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
And other tables just contain a simple id and title records.
Here is the result of EXPLAIN query:
These results are on my local database.
LIKE with a leading wild card is inefficient -- and will scan the entire table, hence the "slower as it gets bigger". Switch to FULLTEXT.
Pagination via OFFSET is inefficient because it must scan all the rows befoer the few desired. See http://mysql.rjweb.org/doc.php/pagination
Both of these have been discussed on stackoverflow repeatedly. Search for other discussions.

inserting datetime string into MySQL results in 0000-00-00

I'm trying to add job info to my DB from an API.
One of my feeds supplies a date(posted) and an expiration date.
I'm trying to insert both fields (along with others).
The date gets entered fine, however the expiry date gets set to the default 0000-00-00 00:00:00
example data from api:
{
"jobId":30081256,
"expirationDate":"22/08/2016",
"date":"11/07/2016"
}
I parse the dates ready to be entered like this:
$job_date = $job->date;
$timestamp = strtotime(str_replace('/', '-', $job_date));
$date = date("Y-m-d H:i:s", $timestamp);
//expiry
$job_expiry = $job->expirationDate;
$expiry_timestamp = strtotime(str_replace('/', '-', $job_expiry));
$expiry_date = date("Y-m-d H:i:s", $expiry_timestamp);
when I echo the results of $date and $expiry_date, i get:
2016-07-11 00:00:00
and
2016-08-22 00:00:00
to me all looks good and the dates are formatted as sql would expect, but the expiry date does not get set.
I'm not sure if it has something to do with my query (shortened to show the fields in question - all other fields insert correctly):
"INSERT INTO
jobs2 (jobref, date, expiry)
VALUES ('"
.$jobref
."','"
.$date
."','"
.$expiry_date
."') ON DUPLICATE KEY UPDATE
`date` = VALUES(date),
`expiry` = VALUES(expiry)"
Maybe the ON DUPLICATE part? The job may already be in my database, but sometimes it has been refreshed/ updated and the posted date or expiry may be different so I want to keep them updated with the latest values from the API.
whats going on here?
table structure as requested:
CREATE TABLE `jobs2` (
`date` datetime NOT NULL,
`title` varchar(200) COLLATE latin1_general_ci NOT NULL,
`company` varchar(100) COLLATE latin1_general_ci NOT NULL,
`email` varchar(100) COLLATE latin1_general_ci NOT NULL,
`url` varchar(1200) COLLATE latin1_general_ci NOT NULL,
`salarymin` int(20) DEFAULT NULL,
`salarymax` int(20) DEFAULT NULL,
`benefits` varchar(100) COLLATE latin1_general_ci NOT NULL,
`salary` varchar(200) COLLATE latin1_general_ci DEFAULT NULL,
`jobtype` varchar(30) COLLATE latin1_general_ci DEFAULT NULL,
`full_part` varchar(30) COLLATE latin1_general_ci DEFAULT NULL,
`salary_per` varchar(20) COLLATE latin1_general_ci DEFAULT NULL,
`location` varchar(100) COLLATE latin1_general_ci DEFAULT NULL,
`country` varchar(30) COLLATE latin1_general_ci NOT NULL,
`description` varchar(3000) COLLATE latin1_general_ci NOT NULL,
`category` varchar(100) COLLATE latin1_general_ci DEFAULT NULL,
`image` varchar(150) COLLATE latin1_general_ci NOT NULL,
`latitude` float NOT NULL,
`longitude` float NOT NULL,
`town` varchar(50) COLLATE latin1_general_ci NOT NULL,
`county` varchar(50) COLLATE latin1_general_ci NOT NULL,
`location_id` varchar(255) COLLATE latin1_general_ci NOT NULL,
`jobref` varchar(100) COLLATE latin1_general_ci NOT NULL,
`partner` varchar(30) COLLATE latin1_general_ci DEFAULT NULL,
`company_id` int(20) DEFAULT NULL,
`featured` varchar(10) COLLATE latin1_general_ci DEFAULT NULL,
`solr_stored` int(1) DEFAULT NULL,
`expiry` datetime NOT NULL,
UNIQUE KEY `location_id` (`location_id`),
KEY `partner` (`partner`),
KEY `company` (`company`),
KEY `title` (`title`),
KEY `company` (`company`),
KEY `longitude` (`longitude`),
KEY `latitude` (`latitude`),
KEY `jobref` (`jobref`),
KEY `jobtype` (`jobtype`),
KEY `company_id` (`company_id`),
KEY `salarymin` (`salarymin`),
KEY `salarymax` (`salarymax`),
KEY `salary_per` (`salary_per`),
KEY `date` (`date`),
FULLTEXT KEY `title` (`title`),
FULLTEXT KEY `company` (`company`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci

Covering Index and two tables in mysql

I've got two tables (NewProducts and OldProducts) that are being compared. NewProducts has about 68,000 records and OldProducts about 51,000. I'm using a covering index on each table, however the query is taking 20 minutes to execute, so I'm not using it properly. Does a covering index really apply with multiple tables? What am I doing wrong? Thank you.
Here is my query code and the indexes:
$querystring = "SELECT newProducts.Id, newProducts.SKU,
newProducts.Title, oldProducts.Title, oldProducts.product_Id
FROM
newProducts, oldProducts
WHERE
trim(newProducts.SKU)=trim(oldProducts.SKU) and
trim(newProducts.Title)=trim(oldProducts.Title) and
oldProducts.Position=1 and
oldProducts.Customer=$shop";
Indexes for NewProducts:
Primary: Id
Index: SKU, Title, customer (not unique)
Indexes for OldProducts:
Primary: Id
Index: Product_id (not unique)
Index: SKU, Title, Postition, Customer (not unique)
?>
CREATE TABLE `NewProducts` (
`Id` bigint(11) NOT NULL,
`Title` varchar(120) COLLATE utf8_unicode_ci NOT NULL,
`Category` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`Office` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`Rehashed` smallint(6) NOT NULL,
`Quantity` smallint(6) NOT NULL,
`Price1` decimal(7,2) NOT NULL,
`Price2` decimal(7,2) NOT NULL,
`Price3` decimal(7,2) NOT NULL,
`Price4` decimal(7,2) NOT NULL,
`created_at` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`OldQuantity` int(11) NOT NULL,
`SKU` varchar(55) COLLATE utf8_unicode_ci DEFAULT NULL,
`Source` varchar(12) COLLATE utf8_unicode_ci NOT NULL,
`customer` varchar(70) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `I-T-S` (`ItemId`,`Title`,`SKU`),
KEY `customer` (`customer`),
KEY `Title` (`Title`,`Rehashed`),
KEY `SKU` (`SKU`),
KEY `Title_2` (`Title`,`SKU`,`customer`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `OldProducts` (
`barcode` varchar(55) COLLATE utf8_unicode_ci DEFAULT NULL,
`compare_at_price` decimal(10,2) DEFAULT NULL,
`created_at` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`fulfillment` varchar(35) COLLATE utf8_unicode_ci DEFAULT NULL,
`grams` decimal(10,2) DEFAULT NULL,
`id` bigint(11) NOT NULL,
`management` varchar(55) COLLATE utf8_unicode_ci DEFAULT NULL,
`policy` varchar(55) COLLATE utf8_unicode_ci DEFAULT NULL,
`size` varchar(55) COLLATE utf8_unicode_ci DEFAULT NULL,
`color` varchar(55) COLLATE utf8_unicode_ci DEFAULT NULL,
`type` varchar(55) COLLATE utf8_unicode_ci DEFAULT NULL,
`position` int(11) DEFAULT NULL,
`price` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`product_id` bigint(11) NOT NULL,
`SKU` varchar(55) COLLATE utf8_unicode_ci DEFAULT NULL,
`Title` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`quantity` int(11) DEFAULT NULL,
`customer` varchar(70) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `P-S-T-PO-CUST`
(`product_id`,`SKU`,`Title`,`position`,`customer`),
KEY `product_id` (`product_id`),
TRIM is the villain. When you hide an indexed column (eg, SKU) inside a function (eg, TRIM), the the index cannot be used.
Clean up your data:
Fix the insertion code to TRIM before inserting (or as it inserts).
UPDATE tbl SET SKU = TRIM(SKU), title = TRIM(title); -- for each table
Change the SELECT: TRIM(SKU) --> SKU etc.
Even Better
oldProducts should have, in this order,
`INDEX(`position`,`customer` ,`SKU`,`Title`, `product_id`)
With this, the WHERE need look only at old rows for position=1 and customer =.... (Actually, the first 2 columns can be in any order; the last 3 in any order.)

mysql php Cannot take foreign key value

Hello I have a problem with my php code.. there are 3 tables for a recruitment system.
CREATE TABLE IF NOT EXISTS `members` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`password` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`cpassword` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`email` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`role` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`uid`)'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=60 ;
The candidate table :
CREATE TABLE IF NOT EXISTS `candidate` (
`fullname` varchar(35) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`webpage` varchar(150) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`tel` int(35) NOT NULL,
`nationality` varchar(35) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`position` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`interviewed` varchar(30) NOT NULL DEFAULT 'No',
`rating` varchar(30) NOT NULL,
`c_id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
PRIMARY KEY (`c_id`),
KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=135 ;
The academic table :
CREATE TABLE IF NOT EXISTS `academic_candidate` (
`degree` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`exp_years` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`comment1` varchar(300) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`proposed_positions` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`research_years` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`comment2` varchar(300) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`department` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`a_id` int(25) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
`c_id` int(11) NOT NULL,
PRIMARY KEY (`a_id`),
UNIQUE KEY `id` (`a_id`),
KEY `uid` (`uid`),
KEY `c_d` (`c_id`),
KEY `c_id` (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;`
-- Constraints for table academic_candidate
ALTER TABLEacademic_candidate
ADD CONSTRAINTacademic_candidate_ibfk_1FOREIGN KEY (uid) REFERENCESmembers(uid) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINTacademic_candidate_ibfk_2FOREIGN KEY (c_id) REFERENCEScandidate(c_id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table candidate
ALTER TABLEcandidate
ADD CONSTRAINTcandidate_ibfk_1FOREIGN KEY (uid) REFERENCESmembers(uid) ON DELETE CASCADE ON UPDATE CASCADE;
--
Now, I use this query in order to store the values in the table academic_candidate
session_start();
$query = "SELECT * FROM academic_candidate WHERE degree = '$degree'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count > 0){
echo "You ALready complete the form </br>";
header("Location:../candidate/candidate_index.php");
}
else{
$degree =($_POST['degree']);
$exp_years = ($_POST['exp_years']);
$comment1 = ($_POST['comment1']);
$proposed_positions = ($_POST['proposed_positions']);
$research_years=($_POST['research_years']);
$comment2=($_POST['comment2']);
$department=($_POST['department']);
$uid=($_SESSION['uid']);
$query1 = "INSERT INTO academic_candidate
(degree,exp_years,comment1,proposed_positions,research_years,comment2,department,uid,c_id)
SELECT
'$degree','$exp_years','$comment1','$proposed_positions','$research_years','$comment2','$department','$uid','$c_id'
FROM candidate
WHERE uid='$uid' AND c_id='$c_id' ";
$result = mysql_query($query1);
if(!$result){
echo "Error";
die (mysql_error());
}
else{
header("Location:../candidate/view_application.php");
}
}
My problem is that stores all the values on the table academic_candidate table but the c_id is 0 . What can I do in order to take the candidate.c_id?
You're passing literal values to your INSERT INTO statement.
Consider the difference:
SELECT 'bar' FROM foo;
SELECT bar FROM foo;
Here's a demo on SQLFiddle.
Looking at your query:
SELECT '$degree'
Is not the same as:
SELECT degree
One of these is simply echoing whatever value you pass in, the other is actually selecting a column from the DB.

PHP MySQL CREATE TABLE

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

Categories