不得不补:PHP的JSON, SQL
不管怎么说,还是得感谢慕课网,提供了很多免费的视频教学。
学习自: https://www.imooc.com/view/68
前端页面:
<!DOCTYPE html> <html> <head> <title>第二个练习</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <script src="jquery-1.8.3.min.js"></script> <script> $("document").ready(function(){ var url = 'ser2.php'; var data={'do':'first'};
// getJSON() 方法使用 AJAX 的 HTTP GET 请求获取 JSON 数据。 $.getJSON(url,data,function(res){ $("#username").val(res.username); $("#password").val(res.password); }); var data={'do':'second'}; $.getJSON(url,data,function(res){ $("#members").val(res[1].username + '\n' + res[2].username + '\n' + res.three.members.username); }); var data={'do':'third'}; $.getJSON(url,data,function(res){ $("#address").val(res.address['2'].address + '\n' + res.address.three.members.username); }); }); </script> </head> <body> <h2>一维数组练习</h2> <input type="text" name="username" id="username" /><br /> <input type="text" name="password" id="password" /><br /> <h2>多维数组练习(一组会员信息)</h2> <textarea id="members" style="width:200px;height:60px"></textarea> <h2>对象实战(一组地址信息)</h2> <textarea id="address" style="width:300px;height:60px"></textarea> </body> </html>
后端页面, 也就是前端所请求的 ser2.php :
<?php
//示例数据 $member['username'] = 'alex'; $member['password'] = '123456'; $do = $_REQUEST['do']; $members['1']['username'] = 'Thanos'; $members['1']['password'] = 'wuxianshoutao'; $members['2']['username'] = '雷神'; $members['2']['password'] = 'thor'; $members['2']['address'] = 'arsjade'; $members['three']['members']['username'] = '这是该死的第三个'; class addressClass{ public $address=array(); public function setAddress($array){ $this->address = $array; } public function getAddress(){ return $this->address; } } $addressObj = new addressClass(); $addressObj->setAddress($members); switch($do){ case 'first': echo json_encode($member); break; case 'second': echo json_encode($members); break; case 'third': echo json_encode($addressObj); break; default: break; }
不得不补的小知识,也得花点时间。
-------------------------------------分割线-------------------------------------
将JSON转为JS对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>为 JSON 字符串创建对象</h2> <p id="demo"></p> <script> var text = '{ "sites" : [' + '{ "name":"Runoob" , "url":"www.runoob.com" },' + '{ "name":"Google" , "url":"www.google.com" },' + '{ "name":"Taobao" , "url":"www.taobao.com" } ]}'; obj = JSON.parse(text); document.getElementById("demo").innerHTML = obj.sites[0].name + " " + obj.sites[0].url; var x; for (x in obj.sites){ document.write(obj.sites[x]['name'] +" | " + obj.sites[x]['url'] +'<br />'); } </script> </body> </html>
将** 转为JSON
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <p id="demo"></p> <script> var str = {"name":"菜鸟教程", "site":"http://www.runoob.com"} str_pretty1 = JSON.stringify(str) document.write( "只有一个参数情况:" ); document.write( "<br>" ); document.write("<pre>" + str_pretty1 + "</pre>" ); document.write( "<br>" ); str_pretty2 = JSON.stringify(str, null, 4) //使用四个空格缩进 document.write( "使用参数情况:" ); document.write( "<br>" ); document.write("<pre>" + str_pretty2 + "</pre>" ); // pre 用于格式化输出 </script> </body> </html>
SQL 语句的几个小知识点:
1. 取某列varchar中第3位仅为数字的,用like肯定比substring要快:
SELECT a.WONO from [dbo].[Temp] as a where a.WONO like '__[0-9]%' -- like 中的 下划线表示单个字符,所以用了2个占2位, [0-9]表示这个位置仅为数字, % 不用解释了吧。
看着有点正则表达式的样子,那么 Like '__[^0-9]%' 就表示不是数字的。
2. 两列nchar 拼接时用+号连接,去空格时只能用 LTRIM 和 RTRIM
3. 数字型的日期'20180501' 想要计算距今天数,需要先用CAST转换为日期。
SELECT DATEDIFF(day, CAST(RTRIM(OPNDT8) as datetime),GETDATE()) as Days --得到距今天数
反过来,日期转为8位数字: CONVERT(VARCHAR(8),GETDATE(),112)
4. 使用Exists代替直接in:
select * from emp where deptno in(select deptno from dept ); select * from emp e where exists(select 1 from dept d where d.deptno=e.deptno );