IoT全棧之畢業設計4:用php操作mysql數據庫

require.php

<?php
define("DB_HOST", "數據庫地址加端口,本地就是localhost");
define("DB_NAME", "數據庫名");
define("DB_USER", "登錄用戶名");
define("DB_PASS", "登陸密碼");
?>

firstphp.php 

<?php
require('require.php');//相對路徑此文件與require.php在同一個文件夾下
if(!isset($_POST["id"])){exit();}
$id=$_POST['id']; //接收前臺傳來的post數據
try {
	$DB_HOST=DB_HOST;
	$DB_NAME=DB_NAME;
	$conn = new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME", DB_USER, DB_PASS);
	// 設置 PDO 錯誤模式爲異常
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
	// 預處理 SQL 並綁定參數、查詢是否已有此帳號
	$stmt1 = $conn->prepare("select * from user WHERE id = :id");
	//從user數據表中查詢id爲前端傳值來的id的數據全部字段
	$stmt1->bindParam(':id', $id);
	$stmt1->execute();
	// 設置結果集爲關聯數組	
	$result = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
	$rows = $stmt1->fetch();
	$user[]= array(
		'id' => $rows['id'],
		'name' => $rows['name'],
		'phone' => $rows['phone']
		);//返回你需要的基本信息
	$arr = array('ec' => 0,'msg' => 'success','user' => $user);//生成一個json
	//echo json_encode($arr);//在顯示器上打印出來
	exit(json_encode($arr));//傳值傳回前端
}
catch(PDOException $e)
{
	$arr = array('ec' => -99,'msg' => '數據庫錯誤:'.$e->getMessage());
	exit(json_encode($arr));
}
//$conn = null; //使用其手動關閉連接
?>

index.vue

<template>
	<view class="login-form">
		<input v-model="chuanzhi" class="input"/>
		<button class="login-btn" @click="tophp">發送</button>
		<text>{{message}}</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				chuanzhi: '',
				message: ''			
			}
		},
		methods: {
			tophp() {
				let id = this.chuanzhi;
				let headers = {};
				headers['content-type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
				uni.request({
					url: 'http://localhost/firstapp/firstphp.php',
					method: 'POST',//get或post
					header: headers,
					data: {
						id:id
					},
					success: res => {
						let data = res.data;
						if (data.ec === 0) {
							this.message=data;//.user;
						} else {
							this.message='暫無數據';
						}
					},
					fail: () => {
						this.message = "網絡連接失敗";
					},
					complete: () => {}
				});
			}
		}
	}
</script>


<style>
	.input{
		background: #e2f5fc;
		margin-top: 30upx;
		border-radius: 100upx;
		padding: 20upx 40upx;
		font-size: 36upx;
	}
	.login-input{
		color: #94afce;
	}
	.login-btn{
		background: #4191ea;
		color: #fff;
		border: 0;
		border-radius: 100upx;
		font-size: 36upx;
		margin-top: 100upx;
	}
	.login-btn:after{
		border: 0;
	}
	/*按鈕點擊效果*/
	.login-btn.button-hover{
		transform: translate(1upx, 1upx);
	}
</style>

效果

數據庫簡單語法

 

查詢 SELECT * FROM table WHERE id = '1'
插入 INSERT INTO table (id, name) VALUES ('2', 'lee')
刪除 DELETE FROM table WHERE id='1'
修改 UPDATE table SET name='le' WHERE id='2'
依照某字段排序 ORDER BY id DESC(從大到小) ASC(從小到大)
且 and
或 or
獲取近一個月的數據 WHERE  DATE_SUB(CURDATE(),INTERVAL 0 MONTH)>=mytime and DATE_SUB(CURDATE(),INTERVAL 1 MONTH)<=mytime
混合操作 SELECT user.id,user.name,userinfo.phone from user,userinfo WHERE user.id=userinfo.id and user.name='lee' ORDER BY id DESC

 

發佈了65 篇原創文章 · 獲贊 6 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章