connecting Databases in PHP

Source: Internet
Author: User

Step1: Link Database mysqli_connect ()Parameters: ① host address ②mysql user name ③mysql password ④ Select connection Database ⑤ port number returned: if connected to  Returns the glyph of the resource type, or False if the connection fails. If we have more than one connection to MySQL, then the various functions that operate the database in the future must pass in the returned connection symbols, and if we have only one connection to MySQL, then the various functions of the database will not have to pass in this sign. Recommendations are passed in. Password is blank 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 successfully returned 0 mysqli_connect_error (); Returns the last connection to the database error message if (Mysqli_connect_errno ($conn)) {die ("Database connection failed!    Failure message: ". Mysqli_connect_error ($conn));} The previous two-step merge (STEP1+STEP2): Connect to the database at the same time to determine $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)Parameters: ① identifier ② connection database name connection succeeded, return true; Connection failed, return FALSE if the database is modified successfully, the database in the resource identifier changes, and if the modification fails without the code termination, subsequent code can continue to execute using the original database Mysqli_ 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 cannot be utf-8 mysqli_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 successfully returns the number of rows affected at the last operation Mysqli_affected_rows ($link) If it is a (DQL) query, returns the number of rows in the resource result set returned by the resource result sets Mysql_num_rows   ($result) Returns the number of fields in the resource result set Mysql_num_fields ($result) If the query fails, returns false $res = Mysqli_query ($conn, $sql); MYSQLI_INSERT_ID ($conn); The Execute INSERT statement returns the last inserted primary key ID STEP7: Parsing result setsVar_dump (Mysqli_fetch_array ($res)); Processes the result set, returns the associative array and the indexed array of ① parameters, the result set to be processed    the parameter ② returns which array format MYSQL_ASSOC-associative array mysql_num-numeric array Mysql_both-default. Simultaneous generation of associative and numeric arrays echo "<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 the associated array pointer to traverse common echo "<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 array of indexes    Var_dump (Mysqli_fetch_object ($res)); Return Objects    Var_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); To close a database connection

connecting Databases 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.