Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts

    Mysql + perl http 500 error

    I get Http 500 Internal Server Error. What is the problem here?

    Code:
    #!/usr/bin/perl
    
    use Mysql;
    
    $host = "localhost";
    $user = "guest";
    $pw = "password";
    $database = "database";
    $tablename = "inventory";
    
    $connect = Mysql->connect($host, $database, $user, $pw);
    $connect->selectdb($database);
    $myquery = "INSERT INTO $tablename (id, name, email, phone, message) VALUES (DEFAULT,'pineapples', 'liqingj@gmail.com', '203-435-1333', '15')";
    $execute = $connect->query($myquery);
    $affectedrows = $execute->affectedrows($myquery);
    $lastid = $execute->insertid($myquery);

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by camaross View Post
    I get Http 500 Internal Server Error. What is the problem here?
    as usual, missing strict and warnings,

    never assume that everything work as you expect, check arguments and values returned and log or print message when something go wrong.
    probably is a mysql error but this is a guess.

    best regards

  • Users who have thanked oesxyl for this post:

    camaross (04-20-2011)

  • #3
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Here's how I would recommend that you do that type of operation. Note the use of DBI and placeholders and of course use strict and use warnings.

    Code:
    #!/usr/bin/perl
    
    use DBI;
    
    use strict;
    use warnings;
    
    my $host = "localhost";
    my $user = "guest";
    my $pw = "password";
    my $database = "database";
    my $tablename = "inventory";
    
    my $dbh = DBI->connect("dbi:mysql:$database:$host";, $user, $pass)
    	or die "DB connect failed: $DBI::errstr";
    
    my $sth = $dbh->prepare(qq{INSERT INTO $tablename (name, email, phone, message) VALUES (?, ?, ?, ?)});
    $sth->execute(qw(pineapples liqingj@gmail.com 203-435-133 15)) or die $dbh->errstr;
    my $lastid = $sth->{mysql_insertid};

  • Users who have thanked miller for this post:

    camaross (04-20-2011)

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by miller View Post
    Here's how I would recommend that you do that type of operation. Note the use of DBI and placeholders and of course use strict and use warnings.

    Code:
    #!/usr/bin/perl
    
    use DBI;
    
    use strict;
    use warnings;
    
    my $host = "localhost";
    my $user = "guest";
    my $pw = "password";
    my $database = "database";
    my $tablename = "inventory";
    
    my $dbh = DBI->connect("dbi:mysql:$database:$host";, $user, $pass)
    	or die "DB connect failed: $DBI::errstr";
    
    my $sth = $dbh->prepare(qq{INSERT INTO $tablename (name, email, phone, message) VALUES (?, ?, ?, ?)});
    $sth->execute(qw(pineapples liqingj@gmail.com 203-435-133 15)) or die $dbh->errstr;
    my $lastid = $sth->{mysql_insertid};
    name can be or not but message is usualy composed of few words separated with spaces, qw is not a good idea in my opinion.

    best regards

  • Users who have thanked oesxyl for this post:

    camaross (04-20-2011)

  • #5
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by oesxyl View Post
    name can be or not but message is usualy composed of few words separated with spaces, qw is not a good idea in my opinion.

    best regards
    Of course, but it was just fake data anyway. The main thing I wanted was to be sure that the @gmail.com wasn't interpolated like it was in his original post.

    - M

  • Users who have thanked miller for this post:

    camaross (04-20-2011)

  • #6
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thank you all very much for the suggestions.

    I will test it tonight. Thanks.


    Quote Originally Posted by miller View Post
    Here's how I would recommend that you do that type of operation. Note the use of DBI and placeholders and of course use strict and use warnings.

    Code:
    #!/usr/bin/perl
    
    use DBI;
    
    use strict;
    use warnings;
    
    my $host = "localhost";
    my $user = "guest";
    my $pw = "password";
    my $database = "database";
    my $tablename = "inventory";
    
    my $dbh = DBI->connect("dbi:mysql:$database:$host";, $user, $pass)
    	or die "DB connect failed: $DBI::errstr";
    
    my $sth = $dbh->prepare(qq{INSERT INTO $tablename (name, email, phone, message) VALUES (?, ?, ?, ?)});
    $sth->execute(qw(pineapples liqingj@gmail.com 203-435-133 15)) or die $dbh->errstr;
    my $lastid = $sth->{mysql_insertid};

  • #7
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Guys, I could not even get this work.

    I am pretty sure the DBD::mysql is installed on the server. And all the log in credentials are correct.

    Code:
    #!/usr/bin/perl
    
    use DBI;
    use strict;
    use warnings;
    
    my $database = "xxx";
    my $user = "xxx";
    my $pw = "xxx";
    
    my $dsn = "DBI:mysql:$database:localhost";
    
    my $dbh = DBI->connect($dsn, $user, $pw)
    	or die "DB connect failed.";

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by camaross View Post
    Guys, I could not even get this work.

    I am pretty sure the DBD::mysql is installed on the server. And all the log in credentials are correct.
    database $database exists in mysql?

    Code:
    #!/usr/bin/perl
    
    use DBI;
    use strict;
    use warnings 'all';
    
    my $database = "xxx";
    my $host = "xxx";
    my $user = "xxx";
    my $pw = "xxx";
    
    my $dsn = "DBI:mysql:$database:$host";
    
    my $dbh = DBI->connect($dsn, $user, $pw, { RaiseError => 1})
    	or die $DBI::errstr;
    best regards

  • Users who have thanked oesxyl for this post:

    camaross (04-21-2011)

  • #9
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Yes, I created the database, added a username and password, then created the table, and specified the headers for each columns.

    Quote Originally Posted by oesxyl View Post
    database $database exists in mysql?

    Code:
    #!/usr/bin/perl
    
    use DBI;
    use strict;
    use warnings 'all';
    
    my $database = "xxx";
    my $host = "xxx";
    my $user = "xxx";
    my $pw = "xxx";
    
    my $dsn = "DBI:mysql:$database:$host";
    
    my $dbh = DBI->connect($dsn, $user, $pw, { RaiseError => 1})
    	or die $DBI::errstr;
    best regards

  • #10
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by camaross View Post
    Yes, I created the database, added a username and password, then created the table, and specified the headers for each columns.
    @camaross

    What type of error message do you get? You're code looks good, so all we can really advise you is to continue to look at the error messages and respond appropriately. Verifying that the database exists, then that the user and password have access to that particular database, etc.

    Quote Originally Posted by oesxyl View Post

    Code:
    use warnings 'all';
    @oesxyl

    Is there a reason why you explicitly specified 'all' for use warnings? Is that just a way to document? As I'm pretty sure that 'all' is default or at least is for current versions of warnings.

    - Miller

  • Users who have thanked miller for this post:

    camaross (04-21-2011)

  • #11
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thank you for the reply. The error code I got is still the same "http 500".

    I am using remote servers. Where can I see the error messages?

    Quote Originally Posted by miller View Post
    @camaross

    What type of error message do you get? You're code looks good, so all we can really advise you is to continue to look at the error messages and respond appropriately. Verifying that the database exists, then that the user and password have access to that particular database, etc.



    @oesxyl

    Is there a reason why you explicitly specified 'all' for use warnings? Is that just a way to document? As I'm pretty sure that 'all' is default or at least is for current versions of warnings.

    - Miller

  • #12
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by camaross View Post
    Thank you for the reply. The error code I got is still the same "http 500".

    I am using remote servers. Where can I see the error messages?
    Do you have access to a command line interface? You should run any cgi script from the command line if possible before trying to access it from the web. This is the quickest way to get feedback about basic syntax errors, stricture errors, and other various warnings.

    To see those errors on a web interface, you need to look at the error_log and/or put the following line at the top of your script:

    Code:
    use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

  • Users who have thanked miller for this post:

    camaross (04-21-2011)

  • #13
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thank you very much!

    Quote Originally Posted by miller View Post
    Do you have access to a command line interface? You should run any cgi script from the command line if possible before trying to access it from the web. This is the quickest way to get feedback about basic syntax errors, stricture errors, and other various warnings.

    To see those errors on a web interface, you need to look at the error_log and/or put the following line at the top of your script:

    Code:
    use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

  • #14
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by miller View Post
    @oesxyl

    Is there a reason why you explicitly specified 'all' for use warnings? Is that just a way to document? As I'm pretty sure that 'all' is default or at least is for current versions of warnings.

    - Miller
    is more a habit, version number is relative, i use 5.10.1 for example

    best regards

  • #15
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by oesxyl View Post
    is more a habit, version number is relative, i use 5.10.1 for example

    best regards
    Yeah, I use 5.8, 5.10, and 5.12 currently depending on the system, but I just hadn't ever seen someone use that before. It's obviously not a big deal, since it will do that be default anyway. But given that new coders are sometimes reluctant to use strictures and warnings, to their own detriment, I wouldn't want to give them any additional reason to avoid them. "Oh, no! Not 'all' the warnings! OMG!".

    - Miller


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •