PHP Connection Database

Source: Internet
Author: User

There is a fixed framework for doing the project, so we don't need to understand the operation function that connects the database. But the interview is often asked, the author recently reviewed the training period of the six-pulse sword hope to be useful to everyone

The approximate code is as follows

<?PHP//PHP Connection Database Six-pulse Excalibur//1 Import profile    include("dbconfig.php");//1 Connection information for the database//2 connection database    $link=mysql_connect(Host,user,pass) or die("Database connection failed");//2 If the connection fails, the failure message is returned//Var_dump ($link);//php Connection Database Returns a resource//3 selecting a database setting character set        mysql_select_db(DBNAME,$link);//3 When choosing a database, we suggest you take $link with you .Mysql_set_charset ("UTF8");//set names UTF8; 4 Write SQL statement to get result set    $sql= "SELECT * from Stu";//4 SQL statement is a string mysql command    $result=mysql_query($sql,$link);//suggest that you take $link with//Var_dump ($result);//The result set obtained is a resource//5 parse result set     while($CC=Mysql_fetch_assoc($result)){//FETCH FETCH ASSOC Get associative array//Print_r ($CC);        Print_r($CC); Echo"; }    //6 Closing the database release result set    Mysql_close($link); Mysql_free_result($result);

This code introduces a dbconfig file code as follows

<? PHP     // profile information used by connection database    //hostname user name password database    Define ("HOST", "localhost");     Define ("USER", "root");     Define ("PASS", "921603540");     Define ("DBNAME", "php04");

Do you understand the essence? Here are some common functions related to this

Mysql_error();//return error message Error mysql_error (); no parameters insideMysql_errno();//returns the error number no No. mysql_errno (); no parameters insideMysql_fetch_row() parse result set gets:indexed ArrayMysql_fetch_assoc() parse result set gets:Associative ArraysMysql_fetch_array() parse result set gets:indexes and associative arraysMysql_fetch_array(, Mysql_both) resolves the result set to get:indexes and associative arraysMysql_fetch_array(, mysql_num) resolves the result set to get:indexed ArrayMysql_fetch_array(, MYSQL_ASSOC) resolves the result set to get:Associative ArraysMysql_fetch_object() parse result set: Associative array

PHP Connection Database

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.