Re: Images
by sch (Pilgrim) on Oct 24, 2002 at 10:06 UTC
|
Have you some code that is giving you problems - if so, post here and we'll see what we can do. Otherwise if you can give us some information on why you can't insert the images into the HTML - and of course you'll get the recommendation to look at CGI.PM.
But today you took me walking, Through a land that we have lost,
While our children sit at websites, With no access to the cost
| [reply] |
|
|
Hi ,This is the code.
Please help me and correct the script .
thanks,Subhash
email:kbs_subhash@yahoo.com
#!/usr/bin/perl
use DBI;
$user="mydb1";
$password="mydb1";
print "Content-type:text/html\n\n";
my $dbh = DBI->connect("dbi:mysql:mydb", $user, $password) or die "Can
+'t connect to $data_source: $DBI::errstr";
print "successful connection\n";
my $sth = $dbh->prepare( q{SELECT sno, name FROM IMAGE}) or die "Can't
+ prepare statement: $DBI::errstr";
my $rc = $sth->execute or die "Can't execute statement: $DBI::errstr
+";
print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n";
print "Field names: @{ $sth->{NAME} }\n";
while (($sno, $name) = $sth->fetchrow_array) {
print "<h6>$name: $phone</h6>\n";
$line="<img src=".$name;
$line=$line." >";
print "<img src=\"$line\">";
}
$dbh->disconnect;
print "succefully disconnected\n";
print "</body></html>\n";
edited: Thu Oct 24 14:51:47 2002
by jeffa - code tags
| [reply] [d/l] |
|
|
#!/usr/bin/perl
use DBI;
$user="mydb1";
$password="mydb1";
print "Content-type:text/html\n\n";
my $dbh = DBI->connect("dbi:mysql:mydb", $user, $password) or die "Can
+'t connect to $data_source: $DBI::errstr";
print "successful connection\n";
my $sth = $dbh->prepare( q{SELECT sno, name FROM IMAGE}) or die "Can't
+ prepare statement: $DBI::errstr";
my $rc = $sth->execute or die "Can't execute statement: $DBI::errstr";
print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n";
print "Field names: @{ $sth->{NAME} }\n";
while (($sno, $name) = $sth->fetchrow_array) {
print "$name: $phone\n";
$line="<img src=".$name;
$line=$line." >";
print "<img src=\"$line\">";
}
$dbh->disconnect;
print "succefully disconnected\n";
print "</body></html>\n";
OK, so looking at the code, here's my thoughts:
- first of all, I'm going to repeat this - get used to CGI.pm, it'll make your life a lot easier.
- use strict - will catch a lot of errors, such as no definition of $phone
- You've got no <body> tag.
- There's no <P> tags surrounding your text
- You're generating HTML that looks something like <img src="<img src="filename">"> in the while loop
Hey, if there's code up ^^ there ^^, don't blame me if it doesn't work.
But today you took me walking, Through a land that we have lost,
While our children sit at websites, With no access to the cost
| [reply] [d/l] |
|
|
Hello...here are my $0.02...I would use a different method from sth (fetchrow()) and, assuming that you meant to use $sno in place of $phone, would change the code in your while loop thusly:
#!/usr/bin/perl
use DBI;
$user="mydb1";
$password="mydb1";
print "Content-type:text/html\n\n";
my $dbh = DBI->connect("dbi:mysql:mydb", $user, $password) or die "Can
+'t connect to $data_source: $DBI::errstr";
print "successful connection\n";
my $sth = $dbh->prepare( q{SELECT sno, name FROM IMAGE}) or die "Can't
+ prepare statement: $DBI::errstr";
my $rc = $sth->execute or die "Can't execute statement: $DBI::errstr";
print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n";
print "Field names: @{ $sth->{NAME} }\n";
while ( my($sno, $name) = $sth->fetchrow() ) {
print "$name: $sno\n";
print "<img src=\"" . $name . "\" >\n";
}
$sth->finish;
$dbh->disconnect;
print "succefully disconnected\n";
print "</body></html>\n";
| [reply] [d/l] [select] |
|
|
|
|
|
|
The other monks definetely provided you with another way to do it. I'll just point out what seems to me to be wrong :
I think that this part is the problem.
$line="<img src=".$name;
$line=$line." >";
print "<img src=\"$line\">";
Wouldn't you want that last line to be just print $line; ?
| [reply] [d/l] |
Re: Images
by feloniousMonk (Pilgrim) on Oct 24, 2002 at 14:14 UTC
|
Just an aside, it makes me sad when someone posts as Anonymous Monk. Especially when the question is something they, and others, can learn from.
I find having my posts available from my homenode to be valuable, especially so I can go back and see how I've grown as a Perl programmer.
So there's my 2 cents :-)
-felonious | [reply] |
Re: Images
by mce (Curate) on Oct 24, 2002 at 13:18 UTC
|
These errors should be very simple to debug when you execute the cgi scripts from the CLI instead of via the browser.
As mentioned by sch, look at the real content of $filename via the CLI.
---------------------------
Dr. Mark Ceulemans
Senior Consultant
IT Masters, Belgium
| [reply] |
Re: Images
by donniejones18 (Initiate) on Oct 24, 2002 at 16:05 UTC
|
I find the qq{} operation to be very helpful when using perl and html at the same time.
Here's an example that should be very easy for you to implement:
#!/usr/bin/perl -w
use DBI;
use CGI;
print "Content-type: text/html\n\n";
$filename = "./test.png";
$html = qq{<html><body>};
$html .= qq{<img src="$filename" width="300" height="300">};
print $html;
I believe all that should work for ya!
Enjoy! :-) | [reply] [d/l] |
|
|
Hi,
Thanks for your kind response.
I've changed my code.but still ....can't get it.
my new code is aas follows...
#!/usr/bin/perl
use strict;
use DBI;
my ($user,$password,$sno,$name,$datasource,$h);
$user="mydb1";
$password="mydb1";
print "Content-type:text/html\n\n";
my $h = qq{<html><body>};
my $dbh = DBI->connect("dbi:mysql:mydb", $user, $password) or die "Can
+'t connect to ";
my $sth = $dbh->prepare( q{SELECT sno, name FROM IMAGE}) or die "Can't
+ prepare statement: $DBI::errstr";
my $rc = $sth->execute or die "Can't execute statement: $DBI::errstr
+";
while ( my($sno, $name) = $sth->fetchrow() ) {
$h .= qq{$name};
}
$sth->finish;
print $h;
print "</body></html>\n";
$dbh->disconnect;
The images are not appearing .
Edit kudra,
2002-10-27
Added code tags
| [reply] [d/l] |
|
|
#!/usr/bin/perl
use strict;
use DBI;
my ($user,$password,$sno,$name,$datasource,$h);
$user="mydb1";
$password="mydb1";
print "Content-type:text/html\n\n";
my $h = qq{<html><body>};
my $dbh = DBI->connect("dbi:mysql:mydb", $user, $password) or die "Can
+'t connect to ";
my $sth = $dbh->prepare( q{SELECT sno, name FROM IMAGE}) or die "Can't
+ prepare statement: $DBI::errstr";
my $rc = $sth->execute or die "Can't execute statement: $DBI::errstr";
+
while ( my($sno, $name) = $sth->fetchrow() ) { $h .= qq{$name}; }
$sth->finish;
print $h;
print "</body></html>\n";
$dbh->disconnect;
The $name part should be more like this:
$h .= qq{<img src="$name" width="300" height="300">};
The print $h part is displaying the data in $h, but this data must be in html format, meaning that you need html code for the images to show up.
Good luck!
donniejones18
Edit kudra,
2002-10-27
Added line breaks within code
| [reply] [d/l] [select] |
Re: Images
by TGI (Parson) on Oct 25, 2002 at 00:51 UTC
|
The q// and qq// quoting operators and here docs can make it very easy to visually separate your HTML from your Perl code. But why not go all the way and really separate your HTML and your Perl?
Unless your site is rather small and will stay that way, you really, really want to use templates. I know, I learned the hard way.
When you decide to use a templating system, don't roll your own, use one of the many excellent modules on CPAN. I know, I learned the hard way.
Which templating system to use? Either start with something simple like HTML::Template or Text::Template and when you are ready for more you can transistion to something more powerful like HTML::Mason or Template Toolkit. Or you could dive right in and learn one of the more powerful systems now.
By the way, you really do want to use strict, use CGI, and enable warnings and taint checking. The sooner you start using these tools, the happier you will be. Check out Ovid's CGI tutorial, you will be glad you did.
TGI says moo
| [reply] [d/l] [select] |
Re: Images
by Anonymous Monk on Oct 25, 2002 at 13:36 UTC
|
try first a simpl HTML file with <IMG SRC=$filename width='300' height='300'>
If this also isn't working than it's not your perl script but the path to your imagefile which is not correct.
the relativepath to your image file is not the relative path to your CGI file!.
| [reply] |