PHP 脚本显示错误

PHP 脚本显示错误

我正在尝试编写 Web 服务,用于 php-mysql 服务器和 Android 设备之间的数据交换。但服务显示一些错误且无法运行。

这是我的代码

<?php

     include 'config.inc.php';

     // Check whether username or password is set from android  
     if(isset($_POST['username']) && isset($_POST['password']))
     {
          // Innitialize Variable
          $result='';
          $username = $_POST['username'];
          $password = $_POST['password'];

          // Query database for row exist or not
          $sql = 'SELECT * FROM tbl_login WHERE  email = :username AND password = :password';
          $stmt = $conn->prepare($sql);
          $stmt->bindParam(':username', $username, PDO::PARAM_STR);
          $stmt->bindParam(':password', $password, PDO::PARAM_STR);
          $stmt->execute();
          if($stmt->rowCount())
          {
             $result="true";    
          }  
          elseif(!$stmt->rowCount())
          {
                $result="false";
          }

          // send result back to android
          echo $result;
    }

?>

现在我的同事是一名 Android 开发人员,他正在从 Android 端制作登录会话模块,他需要用于注册和登录的 Web 服务。

你能解决错误吗?或者

您能否提供这些服务的一些代码或链接到合适的资源?

答案1

您是否正确包含了配置文件?是否获得了正确的数据库连接?请仔细检查这些内容。

您的源代码看起来不错,但如果您在浏览器中遇到任何错误,请提供您的错误。

您也可以尝试以下代码

<?php

   if($_SERVER['REQUEST_METHOD']=='POST'){
  // echo $_SERVER["DOCUMENT_ROOT"];  // /home1/demonuts/public_html
//including the database connection file
       include_once("config.php");

        $username = $_POST['username'];
    $password = $_POST['password'];

     if( $username == '' || $password == '' ){
            echo json_encode(array( "status" => "false","message" => "Parameter missing!") );
     }else{
        $query= "SELECT * FROM registerDemo WHERE username='$username' AND password='$password'";
            $result= mysqli_query($con, $query);

            if(mysqli_num_rows($result) > 0){  
             $query= "SELECT * FROM registerDemo WHERE username='$username' AND password='$password'";
                         $result= mysqli_query($con, $query);
                     $emparray = array();
                         if(mysqli_num_rows($result) > 0){  
                         while ($row = mysqli_fetch_assoc($result)) {
                                     $emparray[] = $row;
                                   }
                         }
               echo json_encode(array( "status" => "true","message" => "Login successfully!", "data" => $emparray) );
            }else{ 
                echo json_encode(array( "status" => "false","message" => "Invalid username or password!") );
            }
             mysqli_close($con);
     }
    } else{
            echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") );
    }
?> 

如果你想获得工作服务,请访问php 在android中登录并注册教程了解更多详细信息。

注册服务

<?php

   if($_SERVER['REQUEST_METHOD']=='POST'){
  // echo $_SERVER["DOCUMENT_ROOT"];  // /home1/demonuts/public_html
//including the database connection file
       include_once("config.php");

        $name = $_POST['name'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $hobby= $_POST['hobby'];

     if($name == '' || $username == '' || $password == '' || $hobby == ''){
            echo json_encode(array( "status" => "false","message" => "Parameter missing!") );
     }else{

            $query= "SELECT * FROM registerDemo WHERE username='$username'";
            $result= mysqli_query($con, $query);

            if(mysqli_num_rows($result) > 0){  
               echo json_encode(array( "status" => "false","message" => "Username already exist!") );
            }else{ 
             $query = "INSERT INTO registerDemo (name,hobby,username,password) VALUES ('$name','$hobby','$username','$password')";
             if(mysqli_query($con,$query)){

                 $query= "SELECT * FROM registerDemo WHERE username='$username'";
                         $result= mysqli_query($con, $query);
                     $emparray = array();
                         if(mysqli_num_rows($result) > 0){  
                         while ($row = mysqli_fetch_assoc($result)) {
                                     $emparray[] = $row;
                                   }
                         }
                echo json_encode(array( "status" => "true","message" => "Successfully registered!" , "data" => $emparray) );
             }else{
                 echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") );
            }
        }
                mysqli_close($con);
     }
     } else{
            echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") );
    }

 ?>

配置如下

<?php
$host="localhost";
$user="your username";
$password="your password";
$db = "your db name";

$con = mysqli_connect($host,$user,$password,$db);

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }else{  //echo "Connect"; 


   }

?>

相关内容