Perl's first program to connect to the MySQL database

Source: Internet
Author: User

DBI is the total interface, and all database connections use

Select different drivers for different databases

#! C:/perl/bin/
Use dBi;
My $ host = "127.0.0.1 ";
My $ user = "root ";
My $ db_name = "sanshi ";
My $ Pwd = "";
My $ con = DBI-> connect ("DBI: mysql: $ db_name", $ user, $ PWD)
Or die "can't connect to $ dbname, $ DBI: errstr ";
Print "Connect suc/N ";
# My $ query = "select * from user ";
My $ query = QQ (select * from user );
# My $ query = q {select * from user };

My $ result = $ con-> prepare ($ query) |
Die "can't prepare statement: $ DBI: errstr ";
My $ exec = $ result-> execute | die "can't execute statement: $ DBI: errstr ";
While ($ u_id, $ u_name) = $ result-> fetchrow_array)
{
Print "$ u_id <---> $ u_name/N ";
}
$ Con-> disconnect;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.