The following small series for everyone to bring a use of PHP access to MySQL database logic operations and additions and deletions to explain the case. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
PHP Access MySQL Database
<?php//Build Connection object $db = new mysqli ("localhost", "root", "", "0710_test");//write SQL statement $ sql = "SELECT * from student";//detect if the connection database is successful, fail to return "Connection failed" and exit program if (Mysqli_connect_error ()) {die ("Connection Failed");} Executes the SQL statement that returns the result set object $result = $db->query ($sql); Var_dump ($result->num_rows);//judgment result with or without data if ($result->num_rows) {echo "existence data";} Fetch data (query statement)//$arr = $result->fetch_all ();//Get all the data and exist in a two-dimensional array//$arr = $result->fetch_all (MYSQLI_ASSOC);// Gets the associative array in all data//$arr = $result->fetch_array ();//while loops through all the data in the array while ($arr = $result->fetch_array ()) {Var_dump ($ ARR);} $arr = $result->fetch_assoc ();//returns associative array//$arr = $result->fetch_object ();//Column name corresponds to member variable//$arr = $result->fetch_ Row ();//returns the index array//var_dump ($ARR); Add and Subtract statements//adds a data (return value TRUE or false) $sql = "INSERT into student values (' 102 ', ' king ', ' Male ', ' 1987-7-1 ', ' 95033 ')";//delete a piece of data ( The return value is True or false) $sql = "Delete from student where Sname= ' Li June '"; $r = $db->query ($sql); Var_dump ($R);
Additions and deletions of the self-growing list in the database
<?php $db = new mysqli ("localhost", "root", "" "," 0710_info "), if (Mysqli_connect_error ()) {die (" Connection Failed ");} $sql = "INSERT into work values (0, ' P005 ', ' 2005-01-01 ', ' 2010-10-1 ', ' shan Normal University ', ' Ministry of Education ', 2)"; if ($db->query ($sql)) {echo "added successfully "; Insert ID for this data (common) echo $db->insert_id;} else{echo "Add Failed";}? >