標籤:
1.資料庫連接 configmysql.php
<?php $q = mysql_connect("localhost:8889","root","root"); if(!$q) { die(‘Could not connect: ‘ . mysql_error()); } mysql_query("set names utf8"); //以utf8讀取資料 //mysql_select_db("myDB",$q); //選擇資料庫?>
2.POST或GET請求,Json返回 selectuser.php
<? include("configmysql.php"); mysql_select_db("myDB",$q); //選擇資料庫 //GET或POST取的參數或者 $TEMPid=$_GET[‘userid‘]; //$TEMPid=$_POST[‘userid‘]; $sql = "select * from UserinfoTB where userid=".$TEMPid; $query = mysql_query($sql)or die(mysql_error()); while($row = mysql_fetch_array($query)){ $json = $row; } echo json_encode($json);?>
3.多組資料 selectuser.php
<? include("configmysql.php"); mysql_select_db("chepaiDB",$q); //資料庫 $TEMPid=$_GET[‘Driverid‘]; $sql = "select * from Driverinfo where Driverid=".$TEMPid; $query = mysql_query($sql)or die(mysql_error()); while($row = mysql_fetch_array($query)){ $json1 = $row; } $sql2 = "select * from Driverillegalinfo where IDriverid=".$TEMPid." order by Iid desc"; $query2 = mysql_query($sql2)or die(mysql_error()); $json2 = array(); $sum=0; while($row2 = mysql_fetch_array($query2)){ $json2[‘row‘.$sum] = $row2; $sum++; } $newJson = json_encode( array_merge( array(‘info1‘ => $json1), array(‘info2‘ => $json2) ) ); echo $newJson;?>
4.檔案照片上傳 upload_file.php
<? header("Content-type: text/html; charset=UTF-8"); include("configmysql.php"); mysql_select_db("chepaiDB",$q); //資料庫 //POST擷取索引值對參數 $TEMPid=$_POST[‘Driverid‘]; echo "\nDriverid is ".$TEMPid."\n"; foreach ($_FILES as $_eachFile) { //file檔案處理 if ($_eachFile["error"] > 0) { echo "錯誤: " . $_eachFile["error"] . "\n"; } else { echo "檔案名稱: " . $_eachFile["name"] . "\n"; echo "類型: " . $_eachFile["type"] . "\n"; echo "大小: " . ($_eachFile["size"] / 1024) . " Kb\n"; } if (file_exists("uploadimage/" . $_eachFile["name"])) { echo $_eachFile["name"] . " 檔案已經存在. "; } else { move_uploaded_file($_eachFile["tmp_name"], "uploadimage/" . $_eachFile["name"]); echo "檔案已經被儲存到: " . "uploadimage/" . $_eachFile["name"]."\n"; } } /* //file檔案處理 if ($_FILES["file"]["error"] > 0) { echo "錯誤: " . $_FILES["file"]["error"] . "\n"; } else { echo "檔案名稱: " . $_FILES["file"]["name"] . "\n"; echo "類型: " . $_FILES["file"]["type"] . "\n"; echo "大小: " . ($_FILES["file"]["size"] / 1024) . " Kb\n"; } if (file_exists("uploadimage/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " 檔案已經存在. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "uploadimage/" . $_FILES["file"]["name"]); echo "檔案已經被儲存到: " . "uploadimage/" . $_FILES["file"]["name"]."\n"; } */?>
[PHP 作為iOS後台Json格式HTTP通訊及檔案上傳的實現]