Error Messageing

M

mike

I was not getting error messages returned using this script. The insert
failed and I didn't get an error message. I had to look in my
error.log.

Anyone see anything wrongs with this?

use DBI;

$dsn = "DBI:mysql:database=mydb;host=­myhost2";
$dbh -> {RaiseError} = 1;
$dbh=DBI->connect($dsn, "id", "passwd");
if ( !defined $dbh )
{
die "Cannot connect to mySQl $DBI::errstr\n";
}
else
{

$sql = "insert into my_posts ( type, head, head_date, msg,
trans_date, poster ) values ( ?, ?, ?, ?, ?, ? )";

$sth = $dbh->prepare( $sql );
if ( !defined $sth )
{
print "Cannot prepare statement: $DBI::errstr\n";
}
else
{
$sth->bind_param('1', $type);
$sth->bind_param('2', $head);
$sth->bind_param('3', $head_date);
$sth->bind_param('4',$msg);
$sth->bind_param('5', $trans_date);
$sth->bind_param('6', $poster);

eval { $sth->execute; };
if ( $@ )
{
# I should get an error message here
print "Error in the database: $@";
}
else
{
# execute worked was success
print "<center><b>Insert - Complete</b></center>\n";
}
}
#release the statement
$sth->finish;

#disconnect the database
$dbh->disconnect;
}
 
X

xhoster

mike said:
I was not getting error messages returned using this script. The insert
failed and I didn't get an error message. I had to look in my
error.log.

Anyone see anything wrongs with this?

use DBI;

$dsn =3D "DBI:mysql:database=3Dmydb;host=3D=ADmyhost2";
....

If you don't "use strict;", I won't help you.

Xho
 
J

J. Gleixner

mike said:
I was not getting error messages returned using this script. The insert
failed and I didn't get an error message. I had to look in my
error.log.

Anyone see anything wrongs with this?
Yep.

$dbh -> {RaiseError} = 1;
$dbh=DBI->connect($dsn, "id", "passwd");

# Add the following here & hopefully you'll see one thing that
# needs to be corrected.
print "The current value of RaiseError is:", $dbh->{'RaiseError'}, "\n";

Also, for simplicity, you can use bind_columns(), and eliminate the
repetitive bind_param() calls.
 
G

Glenn Jackman

At 2005-04-22 11:13AM said:
Also, for simplicity, you can use bind_columns(), and eliminate the
repetitive bind_param() calls.

Or, simplicitiest, pass the values as parameters to execute()
$sth->execute($type, $head, $head_date, $msg, $trans_date, $poster);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top