本文主要和大家分享PHP資料庫操作基礎入門,希望能協助到大家。
擷取表單資料
_POST[”]擷取POST提交的資料,而
_REQUEST擷取一個類似於map的索引值對資料結構。
1 查詢資料
// 資料庫資訊 $url = "192.168.199.146"; //$url = "localhost"; $username = "root"; $password = "123123"; $db_name = "test"; // 擷取資料 $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);//不能在迴圈語句中,由於每次刪除 row數組長度都減小 for($i=0;$i<$count;$i++){ unset($rows[$i]);//刪除冗餘資料 } array_push($jarr,$rows); } $jobj=new stdclass();//執行個體化stdclass,這是php內建的空類,可以用來傳遞資料,由於json_encode後的資料是以對象數組的形式存放的, //$jobj->total=$result->num_rows;//總數 //擷取總數 $get_all_user_sql="SELECT count(*) as total FROM user"; $result_total = $conn->query ( $get_all_user_sql ); $rows_total=$result_total->fetch_assoc()["total"]; $jobj->total = $rows_total; $jobj->rows=$jarr; //所以我們產生的時候也要把資料存放區在對象中 /*foreach($jarr as $key=>$value){ $jobj->$key=$value; }*/ echo json_encode($jobj);
2 添加資料
$url = "192.168.199.146"; //$url="localhost"; $username = "root"; $password = "123123"; $db_name = "test"; //串連資料庫 $conn = new mysqli ( $url, $username, $password, $db_name ); if ($conn->connect_error) { die ( "db connection error" ); } //儲存資料 $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 "新記錄插入成功"; }else{ echo "插入新記錄失敗"; }
參考:
查詢結果輸出為json格式:https://www.cnblogs.com/yiven/p/6491019.html
批量擷取資料並存入資料:http://bbs.blueidea.com/thread-3056406-1-1.html
擷取表單資料
_POST[”]擷取POST提交的資料,而
_REQUEST擷取一個類似於map的索引值對資料結構。
1 查詢資料
// 資料庫資訊 $url = "192.168.199.146"; //$url = "localhost"; $username = "root"; $password = "123123"; $db_name = "test"; // 擷取資料 $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);//不能在迴圈語句中,由於每次刪除 row數組長度都減小 for($i=0;$i<$count;$i++){ unset($rows[$i]);//刪除冗餘資料 } array_push($jarr,$rows); } $jobj=new stdclass();//執行個體化stdclass,這是php內建的空類,可以用來傳遞資料,由於json_encode後的資料是以對象數組的形式存放的, //$jobj->total=$result->num_rows;//總數 //擷取總數 $get_all_user_sql="SELECT count(*) as total FROM user"; $result_total = $conn->query ( $get_all_user_sql ); $rows_total=$result_total->fetch_assoc()["total"]; $jobj->total = $rows_total; $jobj->rows=$jarr; //所以我們產生的時候也要把資料存放區在對象中 /*foreach($jarr as $key=>$value){ $jobj->$key=$value; }*/ echo json_encode($jobj);
2 添加資料
$url = "192.168.199.146"; //$url="localhost"; $username = "root"; $password = "123123"; $db_name = "test"; //串連資料庫 $conn = new mysqli ( $url, $username, $password, $db_name ); if ($conn->connect_error) { die ( "db connection error" ); } //儲存資料 $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 "新記錄插入成功"; }else{ echo "插入新記錄失敗"; }