PHP link to MySQL

Source: Internet
Author: User
<? PHP

$ User   =   " Root " ;
$ Passwd   =   "Root " ;


$ Connect   =   Mysql_connect ( ' Localhost ' ,   $ User ,   $ Passwd )
Or Die ( ' Database Connection Failed: '   .   Mysql_error ());
Echo   ' <Br> <B> Step 1: </B> the connection is established successfully! <Br> ' ;

$ DB   =   ' Test ' ;
Mysql_select_db ( $ DB ) Or Die ( ' Cocould not select database ( ' . $ DB . ' ) Because: ' . Mysql_error ());
Echo   ' <B> Step 2: </B> connect ( ' . $ DB . ' )! <Br> ' ;


Mysql_close ( $ Connect );
Echo   " Nickname " ;
?>

 

Obtain information in the database

 

<? PHP

$ User   =   " Root " ;
$ Passwd   =   " Root " ;


$ Connect   =   Mysql_connect ( ' Localhost ' ,   ' Root ' ,   'Root' ' )
Or Die ( ' Cocould not connect: '   .   Mysql_error ());
Mysql_query ( " Set names 'gb2312' " , $ Connect );
Echo   ' <B> Step 1: </B> the connection is established successfully! <Br> ' ;

$ DB   =   ' Test ' ;
Mysql_select_db ( $ DB ) Or Die ( ' Cocould not select database ( ' . $ DB . ' ) Because: ' . Mysql_error ());
Echo   ' <B> Step 2: </B> connect ( ' . $ DB . ' )! <Br> ' ;


$ Query   =   ' Select * from user ' ;
$ Result   =   Mysql_query ( $ Query ) Or Die ( ' Query failed: '   .   Mysql_error ());



Echo   " <Br> <B> obtain data... </B> <br> " ;
Echo   " <Table border = '1' width = '000000'> \ n " ;

$ Count   =   0 ;
While ( $ Line   =   Mysql_fetch_array ( $ Result , Mysql_assoc )){
Echo   " \ T <tr> \ n " ;
Foreach ( $ Line   As   $ Col_value ){
Echo   " \ T <TD> $ Col_value </TD> \ n " ;
}
$ Count ++ ;
Echo   " \ T </tr> \ n " ;
}

Echo   " </Table> \ n " ;

If ( $ Count   <   1 ){
Echo   " <Br> no records found in the table. <br> " ;
} Else {
Echo   " <Br> The table contains " . $ Count . " Row record. <br> " ;
}


Mysql_free_result ( $ Result );


Mysql_close ( $ Connect );
?>

 

 

 

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.