Eight steps to connect to a database in PHP

Source: Internet
Author: User

Eight steps to connecting a database Step1: Link Database mysqli_connect () parameter: ① host address②mysql User Name③mysql Password④ Selecting a connected database⑤ Port number return: Returns the glyph of the resource type if the connection was successful, or false if the connection failed. If we have more than one connection with MySQL, then the various functions that operate the database later must pass in the returned connection symbol; If we have only one connection to MySQL, then the various functions that operate the database later do not have to pass this identifier. recommendations are passed in. password is empty can omit password $conn = mysqli_connect ("localhost", "root");$conn = Mysqli_connect ("localhost", "root", "" "," MyDB ");Var_dump ($conn); STEP2: is the test database connection successful? Mysqli_connect_errno () and Mysqli_connect_error ()Mysqli_connect_errno (); Returns the error number of the last connection database error, the connection returned successfully 0mysqli_connect_error (); Returns the error message for the last connection to the databaseif (Mysqli_connect_errno ($conn)) {Die ("Database connection failed! Failure message: ". Mysqli_connect_error ($conn)); } the previous two-step merge (STEP1+STEP2): Connect the database and Judge $conn = Mysqli_connect ("localhost", "root", "" "," mydb ") or Die (" Database connection failed! Failure message: ". Mysqli_connect_error ($conn)); Step3: Select Database mysqli_select_db ($link, $dbname) parameter: ① identifier ② connection database name Connection succeeded, return true; Connection failed, return false If the database is modified successfully, the database in the resource identifier is changed;If the modification fails without terminating the operation through the code, subsequent code can continue with the original databasemysqli_select_db ($conn, "mydb") or Die ("Database selection failed! "); STEP4: Setting character set encoding formatMysqli_set_charset ($link, $charset) can only be set to UTF8 and not utf-8Mysqli_set_charset ($conn, "UTF8") or Die ("Database encoding set failed! "); STEP5: Writing SQL statements $sql = "SELECT * from TB1"; STEP6: Execute SQL statement mysqli_query ($link, $sql) If it is (DML) increment, delete, change, returns whether the Boolean type succeeds returns the number of rows affected by the last operation Mysqli_affected_rows ($link)if it is a (DQL) query, the resource result set is returnedreturns the number of rows in a resource result set mysql_num_rows ($result)returns the number of fields in a resource result set Mysql_num_fields ($result)returns False if the query fails$res = Mysqli_query ($conn, $sql);mysqli_insert_id ($conn); Executes the INSERT statement to return the last inserted primary key ID STEP7: Parsing result setsVar_dump (Mysqli_fetch_array ($res)); processing result sets, returning associative arrays and indexed arrays are not commonly used parameter ① The result set to be processed parameter ② returns which array formatMYSQL_ASSOC-Associative arraysmysql_num-numeric arrayMysql_both-Default. Simultaneous generation of associative and numeric arraysecho "<table border= ' 1 ' style= ' border-collapse:collapse;text-align:center;width:200px; ' > ";echo "<thead bgcolor= ' lightblue ' style= ' color: #ffffff; ' ><td>ID</td><td> name </td><td> Age </td><td> Gender </td></thead> ";While ($row =mysqli_fetch_assoc ($res)) {//returns associative array pointer traversal commonecho "<tr>";foreach ($row as $value) {echo "<td>{$value}</td>"; };echo "</tr>"; }echo "</table>";Mysqli_data_seek ($res, 0); Sets the result set pointer position, this expression resets to 0 var_dump (Mysqli_fetch_object ($res)); Var_dump (Mysqli_fetch_row ($res)); Returns an indexed arrayVar_dump (Mysqli_fetch_object ($res)); return ObjectVar_dump (Mysqli_fetch_fields ($res)); Returns the field information for each column in the result set STEP8: Close resource with result set Mysqli_free_result () and Mysqli_close () Mysqli_free_result ($res);//Release query resource result set Mysqli_close ($conn);//Close database connection

Eight steps to connect to a database in PHP

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.