in reply to Re: Perl and Mysql Queries in thread Perl and Mysql Queries
Here are the error messages:
[Mon Jun 10 12:18:31 2024] [error] [client 73.21.226.229] Premature en
+d of script headers: job_post_test.pl, referer: http://diversitylink.
+com/
[Mon Jun 10 12:18:31 2024] [error] [client 73.21.226.229] Bareword fou
+nd where operator expected at E:\\Pinnacle\\DiversityLink\\cgi-bin\\j
+ob_post_test.pl line 38, near "my$sth =$dbh->prepare("INSERT", refere
+r: http://diversitylink.com/
[Mon Jun 10 12:18:31 2024] [error] [client 73.21.226.229] (Might be
+a runaway multi-line "" string starting on line 37), referer: http://
+diversitylink.com/
[Mon Jun 10 12:18:31 2024] [error] [client 73.21.226.229] Semicolon se
+ems to be missing at E:\\Pinnacle\\DiversityLink\\cgi-bin\\job_post_t
+est.pl line 38., referer: http://diversitylink.com/
[Mon Jun 10 12:18:31 2024] [error] [client 73.21.226.229] Use of ?PATT
+ERN? without explicit operator is deprecated at E:\\Pinnacle\\Diversi
+tyLink\\cgi-bin\\job_post_test.pl line 39., referer: http://diversity
+link.com/
[Mon Jun 10 12:18:31 2024] [error] [client 73.21.226.229] Use of ?PATT
+ERN? without explicit operator is deprecated at E:\\Pinnacle\\Diversi
+tyLink\\cgi-bin\\job_post_test.pl line 39., referer: http://diversity
+link.com/
[Mon Jun 10 12:18:31 2024] [error] [client 73.21.226.229] Use of ?PATT
+ERN? without explicit operator is deprecated at E:\\Pinnacle\\Diversi
+tyLink\\cgi-bin\\job_post_test.pl line 39., referer: http://diversity
+link.com/
[Mon Jun 10 12:18:31 2024] [error] [client 73.21.226.229] Use of ?PATT
+ERN? without explicit operator is deprecated at E:\\Pinnacle\\Diversi
+tyLink\\cgi-bin\\job_post_test.pl line 39., referer: http://diversity
+link.com/
[Mon Jun 10 12:18:31 2024] [error] [client 73.21.226.229] Use of ?PATT
+ERN? without explicit operator is deprecated at E:\\Pinnacle\\Diversi
+tyLink\\cgi-bin\\job_post_test.pl line 39., referer: http://diversity
+link.com/
[Mon Jun 10 12:18:31 2024] [error] [client 73.21.226.229] Use of ?PATT
+ERN? without explicit operator is deprecated at E:\\Pinnacle\\Diversi
+tyLink\\cgi-bin\\job_post_test.pl line 39., referer: http://diversity
+link.com/
[Mon Jun 10 12:18:31 2024] [error] [client 73.21.226.229] syntax error
+ at E:\\Pinnacle\\DiversityLink\\cgi-bin\\job_post_test.pl line 38, n
+ear "my$sth =$dbh->prepare("INSERT INTO ", referer: http://diversityl
+ink.com/
[Mon Jun 10 12:18:31 2024] [error] [client 73.21.226.229] Search patte
+rn not terminated or ternary operator parsed as search pattern at E:\
+\Pinnacle\\DiversityLink\\cgi-bin\\job_post_test.pl line 39., referer
+: http://diversitylink.com/
Here is the entire code: #!C:/Perl/bin/perl -w
##post_jobs.pl
use CGI qw(:standard);
use DBI;
print "Content-type: text/html\n\n";
###use POSIX qw(strftime);
####$shortdate = strftime "%B %d, %Y", localtime;
$JobID=param('JobID');
$EmployerID=param('EmployerID');
$EmployerName=param('EmployerName');
$Title=param('Title');
$Logo=param('Logo');
$City=param('City');
$StateProvince=param('StateProvince');
$Description=param('Description');
$Function=param('Function');
$Sector=param('Sector');
$Country=param('Country');
$Posted=param('Posted');
if ($JobID eq ""||$EmployerID eq ""||$EmployerName eq ""||$Title eq ""
+||$Logo eq ""||$City eq ""||$StateProvince eq ""||$Description eq ""|
+|
$Function eq ""||$Sector eq ""||$Country eq ""||$Posted eq ""||$Logo e
+q "")
{
print "The FORM is INCOMPLETE";
}
else {
my$dbh = DBI->connect('dbi:mysql:jobs_db', "Poster','Poster Password')
+;
my$sth =$dbh->prepare("INSERT INTO jobs (JobID,EmployerID,EmployerName
+,Title,City,StateProvince,Description,Function,Sector,Country,Posted)
Values(?,?,?,?,?,?,?,?,?,?,?)");
$sth ->execute($JobID,$EmployerID,$EmployerName,$Title,$City,$StatePro
+vince,$Description,$Function,$Sector,$Country,$Posted);
$sth ->finish();
$dbh->disconnect ();
}
#!/usr/local/bin/perl
print "Your Ad Was Posted.\n";
exit;
Re^3: Perl and Mysql Queries
by Danny (Hermit) on Jun 10, 2024 at 19:43 UTC
|
At line 37 where you connect you have "Poster'. Perhaps that is just a typo in your post? If I run that code I get the same error as you posted which is caused by that incorrect quoting.
Bareword found where operator expected at /tmp/b.pl line 38, near "my$
+sth =$dbh->prepare("INSERT"
(Might be a runaway multi-line "" string starting on line 37)
Semicolon seems to be missing at /tmp/b.pl line 38.
String found where operator expected at /tmp/b.pl line 39, near "print
+ ""
(Missing semicolon on previous line?)
| [reply] [d/l] |
|
That's going to be the problem. Generally, this sort of thing can be detected with e.g.:
perl -cw filename.cgi
Also, the OP desperately needs to start using git or similar SCM so they can see literally what changed between "it's working" and "it's not working". | [reply] [d/l] |
|
| [reply] |
Re^3: Perl and Mysql Queries
by talexb (Chancellor) on Jun 11, 2024 at 01:56 UTC
|
It's always a good idea (and good defensive programming) to check the results of things as you go. So, I'd write
my $dbh = DBI->connect('dbi:mysql:jobs_db', "Poster',
'Poster Password');
defined $dbh or die "Unable to connect to DB: " . $dbh->errstr;
my $sth =$dbh->prepare("INSERT INTO jobs (JobID,EmployerID,
EmployerName,Title,City,StateProvince,Description,Function,
Sector,Country,Posted) Values(?,?,?,?,?,?,?,?,?,?,?)");
defined $sth or die "Unable to prepare insert: " . $dbh->errstr;
$sth->execute($JobID,$EmployerID,$EmployerName,$Title,$City,
$StateProvince,$Description,$Function,$Sector,$Country,$Posted)
or die "Failed to execute insert: " . $dbh->errstr;
The die statements will leave something informative in your log file, and will give you information about what's failing. And I'm also going to encourage you to add
use strict;
use warnings;
at the top of all of your scripts. Generally, your scripts should compile cleanly, and should not put any warnings in the error log. If there are warnings, fix them.
Alex / talexb / Toronto
Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.
| [reply] [d/l] [select] |
|
Now gettin these errors:
Tue Jun 11 05:09:26 2024 error client 73.21.226.229 DBI connect('jobs_db','poster',...) failed: Client does not support authentication protocol requested by server; consider upgrading MySQL client at E:\\Pinnacle\\DiversityLink\\cgi-bin\\job_post_test.pl line 36., referer: http://diversitylink.com/
Tue Jun 11 05:09:26 2024 error client 73.21.226.229 Can't call method "prepare" on an undefined value at E:\\Pinnacle\\DiversityLink\\cgi-bin\\job_post_test.pl line 37., referer: http://diversitylink.com/ Historically the DBI connect statement worked fine, at least with Mysql 5.7. I haven't a clue as to what the 'Can't call method "prepare' error means by 'undefined value'.
| [reply] |
|
$ perl -E '$x->prepare("foo");'
Can't call method "prepare" on an undefined value at -e line 1.
Your connection statement failed and therefore you don't have a defined database handle. So when you try to call the prepare method you are doing it on something which is undefined as $x is in the snippet above.
All of this is because the connection failed. Fix the first problem first, as always.
Which version of DBD::mysql are you running?
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
Can't call method "prepare" on an undefined value at E:\\Pinnacle\\DiversityLink\\cgi-bin\\job_post_test.pl line 37
You have to admit, that's a pretty clear explanation of what's gone wrong. To translate that from geek-speak to English, what it's saying is that Perl can't call the prepare method on an undefined value. And this goes back to the post I did yesterday that encouraged you to program defensively -- always check what the return status is on calls like this, in order to avoid failures like the one you're seeing.
Imagine you're doing a simple task, like frying some eggs for breakfast. You need to get the eggs out of the fridge, find the pan, turn the ring on, wait for it to warm up, crack open the eggs, season to taste, and serve. Some of these steps depend on the previous steps. If you skip the step where you turn on the ring, the eggs aren't going to cook. If you're out of eggs, nothing much is going to happen -- the pan's warm, you have your seasoning ready, but no breakfast.
The DBI documentation is pretty clear (and I've been using it for 25 years); you have to 1. Connect to the database; if that doesn't work, nothing else matters. 2. Prepare the statement handle; you may have a DB connection, but if your prepare fails, you can't do anything. 3. Execute the query or command, and check the return status; it may have failed (wrong number of bind variables, for example). Sometimes, 4. You also do a fetch using the statement handle. Afterwards, you clean up after yourself and 5. Finish with the statement handle and Disconnect from the database.
I would add that for a test script, it's fine to put the credentials into the script, but for anything you're considering having anybody else use, I strongly advise you put the credentials in a separate file that is not put into version control. Credentials should never be put into version control.
Alex / talexb / Toronto
Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.
| [reply] |
|
|
it means your DBI->connect still didn't work and so your $dbh ended up undefined, which makes $dbh->prepare meaningless, because you're ending up trying to call the "prepare" method of some undefined object instead of the "prepare" method of a database handle.
| [reply] [d/l] [select] |
|
| [reply] |
Re^3: Perl and Mysql Queries
by GrandFather (Saint) on Jun 10, 2024 at 22:08 UTC
|
| [reply] |
|
|