一. XAMPP for Mac組件環(huán)境搭建
XAMPP = Apache + PHP + MySQL
XAMPP支持多個操作系統(tǒng):Windows,Linux,Mac OS X
Code2 是一款試用與Mac電腦的網(wǎng)頁編程工具,具有多語言支持(HTML,PHP,JavaScript,CSS等)
二. Web開發(fā)HTML網(wǎng)頁與HTTP
BS模式:客戶端通過瀏覽器以URL地址的形式向服務(wù)器發(fā)送訪問Web頁面的請求.服務(wù)器做出響應(yīng),并以超文本的格式回傳客戶端所請求的Web頁面
HTML的簡單語法:
<html>
<title>
Hello
</title>
<body>
Hello,this is Carson !
</body>
</html>
三. PHP基本語法與概念
PHP的基本語法:
<?php
// 向客戶端發(fā)送數(shù)據(jù)!
echo("Hello,this is Carosn!");
?>
PHP可以嵌入HTML語法,但是文件后綴名必須為.php
<html>
<title>
Hello
</title>
<body>
Hello,this is Carson !
<?php
// 向客戶端發(fā)送數(shù)據(jù)!
echo("<br/>");
echo("Hello,this is Carosn!");
?>
</body>
</html>
PHP中定義變量時,會自動定義變量類型
$int_var = 1;
$str_var = "Carson";
// gettype($int_var);為獲取變量類型 $后為變量名
$strAppend_var = " is me.";
// PHP中的字符串拼接使用"."
echo($str_var.$strAppend_var);
PHP的分支結(jié)構(gòu):
$int_a = 3;
$int_b = 5;
if ($int_a > $int_b) {
echo($int_a);
} else {
echo($int_b);
}
// 三目運算在PHP中同樣適用
echo($int_a > $int_b ? $int_a : $int_b);
PHP的循環(huán)結(jié)構(gòu):
// for 循環(huán)
for ($i = 0; $i < 10; $i++) {
echo($i);
echo("<br/>");
}
// while循環(huán)
$i = 0;
while ($i < 10) {
echo($i);
echo("<br/>");
$i++;
}
// do-while循環(huán)
$j = 0;
do {
echo($j);
echo("<br/>");
$j++;
} while ($j < 10)
PHP的數(shù)組:
$array_var = array("Carson","is","a","clever","boy");
// 數(shù)組元素個數(shù)
$count = count($array_var);
// 數(shù)組結(jié)構(gòu)發(fā)送
echo(var_dump($array_var));
// 給數(shù)組中添加元素
$array_var[] = "HaHa!";
// 換行
echo("<br/>");
echo(var_dump($array_var));
// 刪除數(shù)組中的元素
unset($array_var[0]);
echo("<br/>");
echo(var_dump($array_var));
// 數(shù)組排序(升序)
sort($array_var);
echo("<br/>");
echo(var_dump($array_var));
// 數(shù)組排序(降序)
rsort($array_var);
echo("<br/>");
echo(var_dump($array_var));
PHP的字典:
$dict_var = array("name"=>"Carson","age"=>"23","gender"=>"Male");
echo(var_dump($dict_var));
echo("<br/>");
// 字典轉(zhuǎn)換成為json串
echo(json_encode($dict_var));
GET請求處理:
$name = $_GET["name"];
$age = $_GET["age"];
$sex = $_GET["sex"];
echo($name."<br/>");
echo($age."<br/>");
echo($sex."<br/>");
POST請求處理:
$name = $_POST["name"];
$age = $_POST["age"];
$sex = $_POST["sex"];
echo($name."<br/>");
echo($age."<br/>");
echo($sex."<br/>");
當(dāng)變量使用_REQUEST修飾則使用GET與POST都行
$name = $_REQUEST["name"];
$age = $_REQUEST["age"];
$sex = $_REQUEST["sex"];
$pwd = $_REQUEST["pwd"];
echo($name."<br/>");
echo($age."<br/>");
echo($sex."<br/>");
echo($pwd."<br/>");
四. MySQL數(shù)據(jù)庫操作與PHP交互
- 判斷連接MySQL是否成功
// 連接數(shù)據(jù)庫(第一個參數(shù)為服務(wù)器地址,第二個參數(shù)為MySQL登錄名,第三個參數(shù)為登陸密碼)
$link = mysql_connect("127.0.0.1","root","");
// 選擇連接的數(shù)據(jù)庫的庫名
$select_db = mysql_select_db("test");
// 進行判斷,如果連接成功,向前段發(fā)送一個連接成功的消息,失敗則發(fā)送失敗消息
if ($link && $select_db) {
echo("數(shù)據(jù)庫連接成功!");
} else {
echo("數(shù)據(jù)庫連接失敗!");
}
// 關(guān)閉數(shù)據(jù)庫
mysql_close($link);
- 查詢表
// 輸入SQL語句進行查詢
$result = mysql_query("select * from userlist where name = '$name'");
// 返回所有結(jié)果行數(shù)(數(shù)據(jù)庫中的伴隨指針或游標(biāo))
$stmt = mysql_num_rows($result);
// 返回結(jié)果
$userInfo = mysql_fetch_array($result);
// 釋放伴隨指針
mysql_free_result($result);
- PHP連接MySQL的設(shè)置登陸的后臺數(shù)據(jù)處理源碼:
<?php
// 連接數(shù)據(jù)庫
$link = mysql_connect("127.0.0.1","root","");
// 連接哪個數(shù)據(jù)庫
$select_db = mysql_select_db("conn",$link);
// 進行判斷是否連接成功
if ($link && $select_db) {
// echo("數(shù)據(jù)庫連接成功!");
// 準(zhǔn)備SQL語句
$result = mysql_query("select * from userlist where name = '$name'");
// 返回數(shù)據(jù)所在行數(shù)
$sum_row = mysql_num_rows($result);
// 得到數(shù)據(jù)
$userInfo = mysql_fetch_array($result);
if ($result) {
// 如果result存在的話,說明用戶存在,則匹配密碼
$user_pwd = $pwd;
$sql_pwd = $userInfo["pwd"];
if ($user_pwd == $sql_pwd) { // 用戶密碼匹配成功!
$success = array("flag"=>"success","name"=>$userInfo["name"],"age"=>$userInfo["name"],"sex"=>$userInfo["sex"]);
echo(json_encode($success));
} else { // 用戶密碼匹配失敗!
$errorDic = array("flag"=>"用戶名或密碼錯誤!");
echo(json_encode($errorDic));
}
} else { // 用戶不存在
$errorDic = array("flag"=>"用戶不存在,請先注冊!");
echo(json_encode($errorDic));
}
} else {
$errorDic = array("flag"=>"數(shù)據(jù)庫連接失敗!");
echo(json_encode($errorDic));
}
// 釋放伴隨指針或游標(biāo)
mysql_free_result($result);
// 關(guān)閉數(shù)據(jù)庫
mysql_close($link);
?>
五.iOS與PHP通訊協(xié)議設(shè)計
Xcode中iOS進行POST請求源碼:
- (void)connectMySQLByPHP {
// 文件地址:
//http://127.0.0.1/Carsontest/testConnection.php
// 文件數(shù)據(jù)
//name=Carson&age=23&sex=Male&pwd=0312
// 獲取文件的url
NSString *urlStr = @"http://127.0.0.1/Carsontest/testConnection.php";
NSURL *url = [NSURL URLWithString:urlStr];
// 創(chuàng)建請求對象(異步Block,POST請求)
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";
// 將數(shù)據(jù)轉(zhuǎn)換成為NSData對象
NSString *dataStr = @"name=Carson&age=23&sex=Male&pwd=0312";
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = data;
// 創(chuàng)建會話對象(單例)
NSURLSession *session = [NSURLSession sharedSession];
// 指定會話模式,為請求數(shù)據(jù)模式
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"請求數(shù)據(jù)失敗!-----%@",error.description);
} else {
NSLog(@"請求數(shù)據(jù)成功!");
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
// JSON解析(解析成為NSDictionary對象)
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dict);
// 將上述轉(zhuǎn)化的NSDictionary的對象轉(zhuǎn)化成為JSON字符串.
BOOL flag = [NSJSONSerialization isValidJSONObject:dict];
if (flag) {
NSData *dataJSON = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
NSString *dataJSONStr = [[NSString alloc] initWithData:dataJSON encoding:NSUTF8StringEncoding];
NSLog(@"%@",dataJSONStr);
} else {
NSLog(@"該JSON對象不能轉(zhuǎn)化成為JSON串!");
}
}
}];
// 開始請求
[dataTask resume];
}