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