如何将我的离子应用程序与 MySQL 数据库连接?
How to connect my ionic app with MySQL database?
我无法连接到数据库,当我使用 phpMyAdmin 创建数据库 (myDb) 时应该使用什么语法。我已将 signup.php 放在服务器上,即 www 文件夹。
如果我在这段代码中犯了一些其他错误,请指出。
signup.html:
<ion-header-bar class="bar-positive">
<h2 class="title">SignUp</h2>
</ion-header-bar>
<ion-view view-title="SignUp" name="signup-view">
<ion-content class="has-header">
<div class="list list-inset">
<label class="item item-input item-floating-label">
<span class="input-label">Enter Username</span>
<input class="form-control" type="text" ng-model="userdata.username" placeholder="Enter Username">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Enter Your Email</span>
<input type="text" ng-model="userdata.email" placeholder="Enter Your Email">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Enter Your Password</span>
<input class="form-control" type="password" ng-model="userdata.password" placeholder="Enter Your Password">
</label>
<button class="button button-block button-calm" ng-click="signUp(userdata)">SignUp</button><br>
<span>{{responseMessage}}</span>
</div>
</ion-content>
</ion-view>
signup.php:
<?php
header("Content-Type: application/json; charset=UTF-8");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$email = $postdata->email;
$password = $postdata->password;
$username = $postdata->username;
$con = mysql_connect("localhost","root",'') or die ("Failed to connect to MySQL: " . mysql_error());;
mysql_select_db('myDb', $con);
$qry_em = 'select count(*) as cnt from users where email="' . $email . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);
if($res['cnt']==0){
$qry = 'INSERT INTO users (name,password,email) values ("' . $username . '","' . $password . '","' . $email . '")';
$qry_res = mysql_query($qry);
if ($qry_res) {
echo "1";
} else {
echo "2";;
}
}
else
{
echo "0";
}
?>
app.js:
.controller('SignupCtrl', function($scope, $http) {
$scope.signup = function () {
var request = $http({
method: "post",
url: "http://localhost/signup.php",
crossDomain : true,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {
username: $scope.userdata.username,
email: $scope.userdata.email,
password: $scope.userdata.password
},
});
request.success(function(data) {
if(data == "1"){
$scope.responseMessage = "Account Created Successfully!";
}
if(data == "2"){
$scope.responseMessage = "Can not Create Account";
}
else if(data == "0") {
$scope.responseMessage = "Email Already Exists"
}
});
}
});
如果 PHP 是必须的,我会建议看看 Slim Framework which is made for creating APIs.Some other solutions that fits here (probably better than PHP & MySQL for this purpose) are Mongo + Express or ParseSDK for JavaScript 也是值得一看的东西。我会推荐 Parse,因为它很容易上手并消除了很多令人头疼的后端问题。
示例使用 ionic 访问 API:
控制器:
app.controller('AppCtrl', function($scope){
$http.get('API_URL')
.then(
function(data){
console.log(data);
$scope.data = data;
// JSON data returned as response
},
function(err){
console.log(err);
$scope.err = err;
// when error occurs
}
);
});
查看:
<ion-content ng-controller="AppCtrl">
<div> {{ data }} {{ err }} </div>
</ion-content>
Example JSON 数据的使用。
使用 $request->email
、$request->password
、$request->username
代替 $postdata->email
、$postdata->password
等...
我无法连接到数据库,当我使用 phpMyAdmin 创建数据库 (myDb) 时应该使用什么语法。我已将 signup.php 放在服务器上,即 www 文件夹。 如果我在这段代码中犯了一些其他错误,请指出。
signup.html:
<ion-header-bar class="bar-positive">
<h2 class="title">SignUp</h2>
</ion-header-bar>
<ion-view view-title="SignUp" name="signup-view">
<ion-content class="has-header">
<div class="list list-inset">
<label class="item item-input item-floating-label">
<span class="input-label">Enter Username</span>
<input class="form-control" type="text" ng-model="userdata.username" placeholder="Enter Username">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Enter Your Email</span>
<input type="text" ng-model="userdata.email" placeholder="Enter Your Email">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Enter Your Password</span>
<input class="form-control" type="password" ng-model="userdata.password" placeholder="Enter Your Password">
</label>
<button class="button button-block button-calm" ng-click="signUp(userdata)">SignUp</button><br>
<span>{{responseMessage}}</span>
</div>
</ion-content>
</ion-view>
signup.php:
<?php
header("Content-Type: application/json; charset=UTF-8");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$email = $postdata->email;
$password = $postdata->password;
$username = $postdata->username;
$con = mysql_connect("localhost","root",'') or die ("Failed to connect to MySQL: " . mysql_error());;
mysql_select_db('myDb', $con);
$qry_em = 'select count(*) as cnt from users where email="' . $email . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);
if($res['cnt']==0){
$qry = 'INSERT INTO users (name,password,email) values ("' . $username . '","' . $password . '","' . $email . '")';
$qry_res = mysql_query($qry);
if ($qry_res) {
echo "1";
} else {
echo "2";;
}
}
else
{
echo "0";
}
?>
app.js:
.controller('SignupCtrl', function($scope, $http) {
$scope.signup = function () {
var request = $http({
method: "post",
url: "http://localhost/signup.php",
crossDomain : true,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {
username: $scope.userdata.username,
email: $scope.userdata.email,
password: $scope.userdata.password
},
});
request.success(function(data) {
if(data == "1"){
$scope.responseMessage = "Account Created Successfully!";
}
if(data == "2"){
$scope.responseMessage = "Can not Create Account";
}
else if(data == "0") {
$scope.responseMessage = "Email Already Exists"
}
});
}
});
如果 PHP 是必须的,我会建议看看 Slim Framework which is made for creating APIs.Some other solutions that fits here (probably better than PHP & MySQL for this purpose) are Mongo + Express or ParseSDK for JavaScript 也是值得一看的东西。我会推荐 Parse,因为它很容易上手并消除了很多令人头疼的后端问题。
示例使用 ionic 访问 API:
控制器:
app.controller('AppCtrl', function($scope){
$http.get('API_URL')
.then(
function(data){
console.log(data);
$scope.data = data;
// JSON data returned as response
},
function(err){
console.log(err);
$scope.err = err;
// when error occurs
}
);
});
查看:
<ion-content ng-controller="AppCtrl">
<div> {{ data }} {{ err }} </div>
</ion-content>
Example JSON 数据的使用。
使用 $request->email
、$request->password
、$request->username
代替 $postdata->email
、$postdata->password
等...