dtfbj24048 2017-08-05 17:15
浏览 31
已采纳

Php webservice循环

I am about to lose my mind.I dont have any php experince and I am struggling about php web service.

Here is my code;

<?php

    private $username2 = "";
    private $password2 = "";

    private $DB_CONNECTION;
    private $servername = "localhost";
    private $username = "root";
    private $password = "";
    private $dbname = "dptest";

    function __construct()
    {
        $this->DB_CONNECTION = mysqli_connect($this->servername, $this->username,
            $this->password, $this->dbname);
    }
function getUserType(){
        $sql = "SELECT usertype FROM `login_test` WHERE username = '". $this->username2."'AND password = '".$this->password2."'";

        $result = mysqli_query($this->DB_CONNECTION,$sql);
        //$value = mysqli_fetch_array($result);
        while(!is_null($value = mysqli_fetch_array($result))){
            return $value['usertype'];
        }


    }

}

This is my function code.The other is my login code;

<?php

include_once 'Authentication.php';
use user\Authentication;

$auth = new Authentication();
$auth->prepare($_POST);
$userStatus = $auth->isUserValidToLogIn();


if ($userStatus) {
    // user existed
    // So log him to main page
    $json['success'] = 1;
    $json['message'] = 'access granted';
    $json['usertype'] = $auth->getUserType();


    echo json_encode($json);
} else {

    $json['success'] = 0;
    $json['message'] = 'error!';


    echo json_encode($json);
}

I am trying to get the user's type but when try to get the data form phpmyadmin local database it only gives the first column's usertype.When I try to get 2nd,3rd,4th so on.. user's usertype it doesnt return anything and blank page shows up on postman app.

Also my database looks like this;

usertype username password
admin     despro    1234
client     test     1234
client    despro2   1234
client    despro3   1234
  • 写回答

1条回答 默认 最新

  • dsmupo6631 2017-08-05 19:13
    关注

    The reason you are only getting one column back is because you only request the one column. In order to get the columns you want you need to explicitly request them in your query or use '*' in order to get all columns back. So your query should look like this in order to get all columns from the data table:

    $sql = "SELECT * FROM `login_test` WHERE username = '". $this->username2."'AND password = '".$this->password2."'";
    

    In general, I highly recommend that you stop using MySQLi extension and start using PHP Data Objects (PDO). It makes it easy to use prepared statements. Which also makes your code safer.

    Then your query could look something like this (this is NOT the complete code):

    // connecting to db
    $pdo = new PDO($dsn, $user, $pass, $opt);
        $sql = 'SELECT * 
                FROM login_test 
                WHERE userName = :username
                AND pass = :password;';
    
        $stmt = $pdo->prepare($sql);
    
        $stmt->bindParam(':username', $username2, PDO::PARAM_STR);
        $stmt->bindParam(':password', $password2, PDO::PARAM_STR);
    
        $res = $stmt->execute();
    
        if ($res) {
            $response["userdata"] = array();
            while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                $myData = array();
                $myData["usertype"] = $row["usertype"];
                $myData["username"] = $row["username"];
    
                array_push($response["userdata"], $myData);
            }
        }
    

    Note that the code above is for returning multiple rows of data. If you just want the one row then use something like this:

    if ($res) {
        $response["userdata"] = array();
        $myData = array();
        $myData["usertype"] = $row["usertype"];
        $myData["username"] = $row["username"];
    
        array_push($response["userdata"], $myData);
    }
    

    removing the 'while' statement.

    You might want to take a look at this answer I gave, recently. It is a comprehensive example of using a webservice from an Android app.

    How to insert all the SQL table data into an array in java [android studio]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)