This question already has answers here:
Access value in result set row where the value comes from a MySQL function call
(2 answers)
Closed 1 year ago.
I have a query selecting table including date field, and I want to convert the date into the format dd/MM/YYYY. I retrieved data like this...
$listNews = "
SELECT p.images
, p.title
, p.category
, DATE_FORMAT(p.create_date, '%d/%M/%Y')
FROM posts p
WHERE p.status = 'Active'
AND p.category = 'Tin Tức'
ORDER
BY id DESC
";
$stmtNews = $sqlCon->prepare($listNews);
$stmtNews->execute();
$newsPanel = $stmtNews->fetchAll();
$stmtNews->closeCursor();
but the result was showing that the variable create_date could not defined, when I take out the DATE_FORMAT It works fine. I do not know where I was wrong. Can you guys help me out that problem.
this is my table structure
DROP TABLE IF EXISTS `posts`;
CREATE TABLE `posts` (
`id` int(50) NOT NULL AUTO_INCREMENT,
`link` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`content` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`author` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`create_date` date NOT NULL,
`status` enum('Active','Unactive') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`category` enum('tin-tuc','su-kien','hoat-dong','huong-dan','ho-tro') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`images` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`, `images`) USING BTREE,
INDEX `image_code`(`images`) USING BTREE,
CONSTRAINT `image_code` FOREIGN KEY (`images`) REFERENCES `uploads` (`code`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compact;
Using %m if you want to output a month as a numeric value.
DATE_FORMAT(posts.create_date, '%d/%m/%Y')
Related
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.
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.
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 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
I have two tables. One stores video information, the other stores the tags associated with that video. They share the common field vid_id. I am trying to fulltext search both tables for matches. The goal is that if there is a match in either table, then all fields with that vid_id be gathered from video.
The problem is my query just crashes with Call to undefined method PDOConnectionFactory::errorInfo(). It should return one row since there is one entry in tags where name field = test. Anyone have any ideas? I have been struggling with this for a while.
CREATE TABLE IF NOT EXISTS `tags` (
`id` varchar(35) NOT NULL,
`vid_id` varchar(35) NOT NULL,
`name` varchar(45) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
UNIQUE KEY `vid_id` (`vid_id`,`name`),
FULLTEXT KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `video` (
`timestamp` int(11) NOT NULL,
`vid_id` varchar(32) NOT NULL,
`file_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`uploader` varchar(55) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`subject_id` int(1) NOT NULL,
FULLTEXT KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
$sql = "SELECT video.*
MATCH(video.title) AGAINST('?') as cscore,
MATCH(tags.name) AGAINST('?') as htscore
FROM video
LEFT JOIN tags ON video.vid_id=tags.vid_id
WHERE
MATCH(video.title) AGAINST('?') OR
MATCH(tags.name) AGAINST('?')
ORDER BY cscore DESC;";
$stmt4 = $conn->prepare($sql);
$result=$stmt4->execute(array('test','test','test','test')) or die(print_r($db->errorInfo(), true));
The problem is that you're missing a comma (,) after SELECT video.* in your SQL query.
Working example:
SELECT video.*,
MATCH(video.title) AGAINST('?') as cscore,
MATCH(tags.name) AGAINST('?') as htscore
FROM video
LEFT JOIN tags ON video.vid_id=tags.vid_id
WHERE
MATCH(video.title) AGAINST('?') OR
MATCH(tags.name) AGAINST('?')
ORDER BY cscore DESC;