This article is mainly to share with you a basic introduction to PHP database operations, hoping to help everyone.
Get form data
_post["] Gets the data of the POST submission, and
_request gets a data structure that resembles a map key-value pair.
1 Querying data
Database Information $url = "192.168.199.146"; $url = "localhost"; $username = "root"; $password = "123123"; $db _name = "Test"; Get Data $conn = new Mysqli ($url, $username, $password, $db _name); if ($conn->connect_error) {die ("DB connection Error"); } $start _rows = ($page-1) * $rows; $sql = "SELECT * from User LIMIT". $start _rows. "," . $rows; $result = $conn->query ($sql); $jarr = Array (); while ($rows =mysqli_fetch_array ($result, Mysql_assoc)) {$count =count ($rows);//cannot be in a looping statement because each deletion of the row array length decreases the for ( $i =0; $i < $count; $i + +) {unset ($rows [$i]);//delete redundant data} array_push ($jarr, $rows); } $jobj =new Stdclass ();//Instantiate Stdclass, which is an empty class built into PHP that can be used to pass data, since the Json_encode data is stored as an array of objects,//$jobj->total=$ result->num_rows;//Total//Get total $get _all_user_sql= "SELECT COUNT (*) as totals from user"; $result _total = $conn->query ($get _all_user_sql); $rows _total= $result _total->fetch_assoc () ["Total"]; $jobj->total = $rows _total; $jobj->rows= $jarr; So when we generate it we also store the data in the object/*foreach ($jarr as $key + $value) {$jobj $key = $value; }*/Echo Json_encode ($jobj);
2 Adding data
$url = "192.168.199.146"; $url = "localhost"; $username = "root"; $password = "123123"; $db _name = "Test"; Connect database $conn = new Mysqli ($url, $username, $password, $db _name); if ($conn->connect_error) {die ("DB connection Error"); } Save The data $insert _sql= "Insert User (Name,password)"; $insert _sql= $insert _sql. " VALUES (' ". $param [' name ']." ', ' ". $param [' Password ']." ') "; echo $insert _sql; $result = $conn->query ($insert _sql); if ($result ===true) { echo "new record inserted successfully"; } else{ echo "Insert new record Failed"; }
Reference:
Query result output to JSON format: https://www.cnblogs.com/yiven/p/6491019.html
Get data in bulk and deposit data: http://bbs.blueidea.com/thread-3056406-1-1.html
Get form data
_post["] Gets the data of the POST submission, and
_request gets a data structure that resembles a map key-value pair.
1 Querying data
Database Information $url = "192.168.199.146"; $url = "localhost"; $username = "root"; $password = "123123"; $db _name = "Test"; Get Data $conn = new Mysqli ($url, $username, $password, $db _name); if ($conn->connect_error) {die ("DB connection Error"); } $start _rows = ($page-1) * $rows; $sql = "SELECT * from User LIMIT". $start _rows. "," . $rows; $result = $conn->query ($sql); $jarr = Array (); while ($rows =mysqli_fetch_array ($result, Mysql_assoc)) {$count =count ($rows);//cannot be in a looping statement because each deletion of the row array length decreases the for ( $i =0; $i < $count; $i + +) {unset ($rows [$i]);//delete redundant data} array_push ($jarr, $rows); } $jobj =new Stdclass ();//Instantiate Stdclass, which is an empty class built into PHP that can be used to pass data, since the Json_encode data is stored as an array of objects,//$jobj->total=$ result->num_rows;//Total//Get total $get _all_user_sql= "SELECT COUNT (*) as totals from user"; $result _total = $conn->query ($get _all_user_sql); $rows _total= $result _total->fetch_assoc () ["Total"]; $jobj->total = $rows _total; $jobj->rows= $jarr; So when we generate it we also store the data in the object/*foreach ($jarr as $key + $value) {$jobj $key = $value; }*/Echo Json_encode ($jobj);
2 Adding data
$url = "192.168.199.146"; $url = "localhost"; $username = "root"; $password = "123123"; $db _name = "Test"; Connect database $conn = new Mysqli ($url, $username, $password, $db _name); if ($conn->connect_error) {die ("DB connection Error"); } Save The data $insert _sql= "Insert User (Name,password)"; $insert _sql= $insert _sql. " VALUES (' ". $param [' name ']." ', ' ". $param [' Password ']." ') "; echo $insert _sql; $result = $conn->query ($insert _sql); if ($result ===true) { echo "new record inserted successfully"; } else{ echo "Insert new record Failed"; }