LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-11-2003, 07:07 PM   #1
rhuser
Member
 
Registered: Jan 2003
Posts: 55

Rep: Reputation: 15
the format of the date in stat in wrong


Hi.

I am using stat to obtain image size and image date, however the format that is coming especially for the date. the format seems wrong,

mysql> select * from imageinfo;
+----------+--------------------+------------+---------------------+
| image_id | image_name | image_size | image_date |
+----------+--------------------+------------+---------------------+
| 1 | 2003test.jpg | 123 | 0000-00-00 00:00:00 |
| 2 | 2003test.jpg | 123000 | 2003-03-10 22:10:14 |
| 3 | 2003test1.jpg | 124000 | 2003-03-10 22:13:48 |
| 4 | 20030311143242.jpg | 1047393161 | 2000-10-47 39:31:62 |
| 5 | 20030311143242.jpg | 1047393161 | 2000-10-47 39:31:62 |
| 6 | 20030311143242.jpg | 1047393161 | 2000-10-47 39:31:62 |
| 7 | 20030311143242.jpg | 1047393161 | 2000-10-47 39:31:62 |
| 8 | 20030311143243.jpg | 1047393161 | 2000-10-47 39:31:62 |
| 9 | 20030311143243.jpg | 1047393161 | 2000-10-47 39:31:62 |
| 10 | 45677890 | 1047422331 | 2000-10-47 42:23:32 |
| 11 | 123456789 | 1047422331 | 2000-10-47 42:23:32 |
| 12 | 1111111111111111 | 1047422331 | 2000-10-47 42:23:32 |
+----------+--------------------+------------+---------------------+
12 rows in set (0.01 sec)



my $file;
my $size;
my $mtime;

$file = "/home/me/images/2003_03_11_22_38_52.jpg";
($size, $mtime) = (stat ($file))[8,9];

my $rows_affected = $dbh->do("INSERT INTO imageinfo VALUES('null','1111111111111111',$size,$mtime)");




Regards,

Mel
 
Old 03-11-2003, 08:41 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
Excuse my ignorance, but what programming language
are we looking at here? If stat is anything similar to
"C in Linux environments" here you're picking the wrong
fields from the structure (assuming that (stat ($file))[8,9]
gets field 8 and 9) ...

Cheers,
Tink
 
Old 03-11-2003, 08:57 PM   #3
rhuser
Member
 
Registered: Jan 2003
Posts: 55

Original Poster
Rep: Reputation: 15
hi

i am using perl
 
Old 03-11-2003, 09:09 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
Try 7 and 8 mate ;)

Cheers,
Tink
 
Old 03-11-2003, 09:13 PM   #5
rhuser
Member
 
Registered: Jan 2003
Posts: 55

Original Poster
Rep: Reputation: 15
i got it, it is working now, i changed the code. however i require some assistance in another side of the script, are you good in perl and db. i hope so as i really need some help.

cheers,
Mel
 
Old 03-11-2003, 09:15 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
I'm not really good at anything :} but know a little about
a whole lot of things ;)

Cheers,
Tink
 
Old 03-11-2003, 09:19 PM   #7
rhuser
Member
 
Registered: Jan 2003
Posts: 55

Original Poster
Rep: Reputation: 15
ok, i will give it a try, i really appreciate your help

i have this script that i would like to take image information from a directory namely /home/me/images/. the information i would like the script to take is the name, size and date, and insert these data into MySQL. Basic, but i have had alot of problems doing it.

---------------------------------
#!/usr/bin/perl -w
use strict;
use DBI;
use Date::Manip;

############################################################################
####
# Connect to Database Named cctvimages on the localhost with the root user
# $dbh=DBI->connect(DBI:mysql;$database", $user, $password);
############################################################################
####

my $dbh = DBI->connect("DBI:mysql:dbname=cctvimages;host=localhost",
"root", "mel", {'RaiseError' => 1});

my $file;
my $size;
my $mtime;
my $secs;


my $dir = '/home/me/images';
my @jpegs = `ls *.jpg`;
foreach (@jpegs) {
print"$_"; # dont forget that a newline is still at the end of each element in the array...
}



$file = "/home/me/images/2003_03_11_14_32_42.jpg";

($size, $secs) = (stat ($file))[8,9];

$mtime = &ParseDateString("epoch $secs"); # even after conversion ':' is used to seperate hh and mn and ss

$mtime =~ s/://g; # the above swaps out the ':' for nothing

$file =~ s/\/home\/me\/images\///; # the above strips path

print"size is $size\nmodified is $mtime\nfilename is $file\n";

my $rows_affected = $dbh->do("
INSERT INTO imageinfo
VALUES(null, '$file', '$size', '$mtime')") or die"Do Fails: $DBI::errstr\n";

my $sql = "SELECT * FROM imageinfo";
my $sth = $dbh->prepare($sql);
$sth->execute or die"Execute fails: $DBI::errstr\n";


$sth->finish;
$dbh->disconnect;
 
Old 03-12-2003, 06:29 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
Humm ... not going to install MySQL just to
test your script and see where there are errors ;)

What's not working?

Cheers,
Tink
 
Old 03-12-2003, 06:33 PM   #9
rhuser
Member
 
Registered: Jan 2003
Posts: 55

Original Poster
Rep: Reputation: 15
Hi

thanks,

it is working now: i am having errors in this:

_____________________


<html>
<body>

<?php

mysql_connect (localhost, root, mel);

mysql_select_db (cctvimages);


$result = mysql_query ("SELECT * FROM imageinfo WHERE image_date>='2003-03-12 22:00:01' AND image_date<='2003-03-12 23:00:00';
");

if ($row = mysql_fetch_array($result)) {

do {
print "<table><tr><td><b>Image_Id</b></td><td><b>Image_Name</b></td><td><b>Image_Size</b></td><td><b>Image_Date</b></td></tr></table>";
print ("");
print $row["image_id"];
print ("");
print $row["image_name"];
print ("");
print $row["image_size"];
print ("");
print $row["image_date"];
print ("<p>");
} while($row = mysql_fetch_array($result));


} else {print "Sorry, no records were found!";}

?>

</body>
</html>

_________________________
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/httpd/htdocs/php/search2.php on line 14
Sorry, no records were found!
 
Old 03-16-2003, 01:46 PM   #10
taivu
LQ Newbie
 
Registered: Oct 2002
Location: Spain
Distribution: Ubuntu, Debian Sarge, FreeBSD
Posts: 19

Rep: Reputation: 0
You are getting the error because $result is empty (no matching records found) but you are still trying to do mysql_fetch_array on it.

Try changing:

if ($row = mysql_fetch_array($result)) {

on row 14 to:

if (is_array($result)) {

That will check if returned recordset is not empty before trying to perform mysql_fetch_array (on row 27) on it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'date' command format Neorio Linux - General 3 01-18-2011 06:18 AM
Date Format and Locale candyman123 Linux - Software 7 01-04-2006 12:58 AM
Thunderbird Date Format dotancohen Linux - Software 9 10-15-2005 03:55 PM
Date format Mik LQ Suggestions & Feedback 8 12-05-2004 11:14 AM
Bug report: wrong date/time format in last visited wipe LQ Suggestions & Feedback 0 07-29-2004 07:21 AM


All times are GMT -5. The time now is 05:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration