最近在做的AutoHotkey中需要访问主机数据库,所以去学习了PHP、SQL、JSON再此分享出来。
本文中所学来自万维网,首先在服务器创建PHP文件,autohotkey.php与ahk文件下方给出代码。
[video width=”1920″ height=”1080″ mp4=”http://yun.autoahk.com/autohotkey/video/coupling-interaction/php/ahk-php-sql-json.mp4″][/video]
header("Content-type:text/html;charset=utf-8");
$con = mysql_connect("localhost:3306","数据库账号","数据库密码"); //链接数据库
mysql_select_db("数据库名",$con); //需要操作的数据库名
mysql_query('SET NAMES UTF8'); //设置数据库编码
$result = mysql_query("SELECT * FROM lib ORDER BY heat DESC",$con); //执行数据库查询命令
$results = array(); //将查询结果转换为数组
while ($row = mysql_fetch_assoc($result))
$results[] = $row;
echo urldecode(json_encode($results,JSON_UNESCAPED_UNICODE)); //输出JSON字符串
mysql_close($con); //关闭数据库连接
?>
#Persistent
#SingleInstance force
#Include %A_ScriptDir%\JSON.ahk
Gui,Add,ListView,w600,脚本名|描述|标签|热度
Gui,Show
get:=JSON.load(Update())
for k,v in get
LV_Add("" , get[A_Index].name,get[A_Index].describe,get[A_Index].label,get[A_Index].heat)
LV_ModifyCol()
return
Update(){
static req := ComObjCreate("Msxml2.XMLHTTP")
req.open("GET","http://zozhu.com/autohotkey.php",false)
req.Send()
return req.responseText
}