Windows Perl Scripting Forums » Perl

DBI-Conenct Problem

(2 posts)
  • Started 12 years ago by shaffiahmed
  • Latest reply from Dave

Tags:


  1. shaffiahmed
    Member

    I have a website which is already working. I have decided to chnage my hosting company. But i am having a problem getting my perl script working.

    If you click on the link below it displays a list of all proeprties:
    http://www.goarealestates.com/cgi-bin/housing.pl?search=1&category=Buy&;submit=Go!!!

    Within my script file, i have a statement as below which connects to the database
    $dbh = db_connect($database,$user,$password);

    And the function db_connect() is as below:-
    sub db_connect {
    ($database,$user,$password) = @_;
    my $h = DBI->connect("DBI:mysql:$database", $user, $password)
    or printError("Unable to connect to $database" . $DBI::errstr);
    return $h;
    }


    I have also got a db.cfg file which is as below
    # script config file
    $database = "shafi";
    $user = "shafi";
    $password ="shafi";
    1;




    But the present hosting provider has given an example of how to connect to the mysql database as below:-
    =================================================
    #!/usr/bin/perl
    use DBI;
    print "Content-type:text/html\n\n";
    $db_handle = DBI->connect("dbi:mysql:database=dbxxxxxxxx;host=dbxxx.oneandone.co.uk;user=dboxxxxxxxx;password=xxxxxxxx")
    or die "Couldn't connect to database: $DBI::errstr\n";

    $sql = "SELECT * FROM puretest";
    $statement = $db_handle->prepare($sql)
    or die "Couldn't prepare query '$sql': $DBI::errstr\n";

    $statement->execute()
    or die "Couldn't execute query '$sql': $DBI::errstr\n";
    while ($row_ref = $statement->fetchrow_hashref())
    {
    print "Name <b>$row_ref->{name}</b> has email address::<b>$row_ref->{email}</b>.<br>";
    }

    $db_handle->disconnect();
    ===================================================

    The DBI->connect statement is different and deosnt seem to work. Could someone tell me how my function should change to adapt to this hsoting provider.
    Posted 12 years ago #
  2. Dave
    Perl guy

    I do not have very much experience with DBI but of the experience I do have the syntax you used seems correct. I use something akin to:


    use DBI;
    my $server ='foo_server';
    my $db ='my_database_name';
    my $usrnme ='my_user_name';
    my $pwd ='my_password';

    my $hDB = DBI->connect("dbi:mysql:$db:$server", $usrnme, $pwd) || die $DBI::errstr;

    my $query="SELECT * FROM ping_lab";
    my $sth = $dbh->prepare($query);
    $sth->execute();
    while (my $row = $sth->fetchrow_arrayref)
    {
        print join("\t", @$row),"\n";
    }



    Have you tried changing the code your host provider gave you?
    Posted 12 years ago #

RSS feed for this topic

Reply

You must log in to post.