如果用户的信息(登录信息)与存储在数据库中的信息匹配,我想制作一个 php 文件以在 android 设备上返回 true 或 false。
到目前为止,我已经编写了下面的代码:
<?php
require "init.php";
if($connection){
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($connection,"SELECT * FROM `admin` WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement,"ss",$username,$password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_close($connection);
}
要返回一个 json,您必须创建一个关联数组并将其编码为 json。
<?php
require "init.php";
require "init.php";
if($connection){
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($connection,"SELECT * FROM `admin` WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement,"ss",$username,$password);
if(mysqli_stmt_execute($statement)){
$response=array('response'=>true);
}else {
$response=array('response'=>false);
}
echo json_encode($response);
mysqli_stmt_store_result($statement);
mysqli_close($connection);
}
简单的mysqli_stmt_execute返回布尔值,只需打印结果即可,或者您可以制作一个 json。
<?php
require "init.php";
require "init.php";
if($connection){
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($connection,"SELECT * FROM `admin` WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement,"ss",$username,$password);
if(mysqli_stmt_execute($statement)){
echo "true";
}else {
echo "false";
}
mysqli_stmt_store_result($statement);
mysqli_close($connection);
}