Data access is divided into three types of 1. Use a function to discard 2 in a new version. An object-oriented approach to the Mysqli class 3.PDO example <table width= "100%" border= "1" > <tr> <td> codename </td> <td> name </td> <td> gender </td> <td> National </td> <td> Birthdays </td> </tr> Use the Mysqli class to access the database: 1. The object in Mysqli is equivalent to establishing a channel between PHP and MySQL database $db = new mysqli ("localhost", "root", " "," Lian "); 2. Determine if the connection is wrong if (Mysqli_connect_error ()) {echo ' Connection failed! "; exit; If there is an error connecting, end the program}3. Write SQL statement $sql = "SELECT * from Info"; 4. Execute the prepared SQL statement $result = $db->query ($sql); If you are executing a query statement, return the result set object. Returns TRUE or FALSE5 if a different statement is executed. Reads data from the result set object $ARR1 = $result->fetch_row (); Each time you read a bar, the returned array can use the while loop to read all the data while ($arr = $result->fetch_row ()) {var_dump ($arr);} $arr = $result->fetch_all (); Read all, return two-dimensional array var_dump ($arr); $arr = $result->fetch_assoc (); Reads a bar, returns the associative array var_dump ($arr); $arr = $result->fetch_object (); Reads a bar, returns the object Var_dump ($arr); $arr = $result->fetch_all (); foreach ($arr as $v) {//processing gender $sex = $v [2]? " Male ":" Female "; Ethnic Processing $sql = "Select name frOm Nation where code= ' {$v [3]} ' "; $re = $db->query ($sql); $a = $re->fetch_row (); echo "<tr> <td>{$v [0]}</td> <td>{$v [1]}</td> & lt;td>{$sex}</td> <td>{$a [0]}</td> <td>{$v [4]}</td> </tr> ";} Then the connection is PHP, as follows $db = new mysqli ("localhost", "root", "" "," Lian "), if (Mysqli_connect_error ()) {die (" Connection failed! ");} $sql = "INSERT into Xuanxiang values (0, ' 111111 ', ' a ', 1)", if ($db->query ($sql)) {echo "added successfully!) "; Echo $db->insert_id; Take the added primary key value}else{echo "Add failed! ";}
Using the MySQL class for data access