java学习php(三)增删改查+json

来源:互联网 发布:淘宝刷流量收藏平台 编辑:程序博客网 时间:2024/05/21 11:00
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><?php//$q=$_GET["q"];class product{private $con ;function __construct(){$this->con = mysql_connect('localhost', 'root', 'root');if (!$this->con){die('Could not connect: ' . mysql_error());}mysql_select_db("appcan", $this->con);}function  query(){$sql="SELECT * FROM product WHERE 1=1";$result = mysql_query($sql)  or die('数据失败!'.mysql_error());;echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th><th>Age</th><th>Hometown</th><th>Job</th><th>json 数据</th></tr>";echo json_encode(mysql_num_fields($result));while($row = mysql_fetch_array($result)){echo "<tr>";echo "<td>" . $row['id'] . "</td>";echo "<td>" . $row['top'] . "</td>";echo "<td>" . $row['title'] . "</td>";echo "<td>" . $row['summary'] . "</td>";echo "<td>" . $row['type'] . "</td>";echo "<td>" . json_encode($row). "</td>";echo "</tr>";}echo "</table>";}function insert($str){$result = mysql_query($str)  or die('数据插入失败!'.mysql_error());}function close(){mysql_close($this->con);}}//以下为直接执行内容。做了一个小判断利用超全局的变量$_GET 获得operate 判断进行简单路由。//header 中文编码处理。 此处可以看出。这是一个全局的函数因为是(), 而$_GET 是一个数组 [] $p = $_GET["operate"];$pro = new product();if($p=='list'){header("Content-Type: text/html; charset=gbk");$pro->query();}else if($p=='insert'){$pro->insert("insert into product values('4','upload/b.png','文章','文章内容','3km','4')");}else{}$pro->close();?>

上面有与数据库的操作, 还是和java差不多, 比java少一些,没有加载驱动什么的。直接连接数据库,也没有statement语句操作。用con对象,操作就ok、

返回json也比较简单,没有什么jsonArray 什么的。直接json_encode(目标)但是也少一些功能,弱类型的简单行,java这种强类型没办法。

0 0
原创粉丝点击