为什么我在php函数中获取storename数据

时间:2016-09-22 09:31:34

标签: php html function mysqli

这是一个在PHP中以echo形式检索用户数据的脚本。

<?php 
include "db_config.php";

class User{     
    public $db;
    public function __construct(){
        $this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

        if(mysqli_connect_errno()) {     
            echo "Error: Could not connect to database."; 
           exit; 
        }
    }

    /*** for registration process ***/
    public function reg_user($ustore, $unik, $name,$username,$password,$email){

        $password = md5($password);
        $sql="SELECT * FROM users WHERE uname='$username' OR uemail='$email' OR unik='$unik'";

        //checking if the username or email is available in db
        $check =  $this->db->query($sql) ;
        $count_row = $check->num_rows;

        //if the username is not in db then insert to the table
        if ($count_row == 0){
            $sql1="INSERT INTO users SET ustore='$ustore', unik='$unik', uname='$username', upass='$password', fullname='$name', uemail='$email'";
            $result = mysqli_query($this->db,$sql1) or die(mysqli_connect_errno()."Data cannot inserted");
            return $result;
        }
        else { return false;}
    }

    /*** for login process ***/
    public function check_login($emailusername, $password){

        $password = md5($password);
        $sql2="SELECT uid from users WHERE uemail='$emailusername' or uname='$emailusername' or  unik='$emailusername' and upass='$password'";

        //checking if the username is available in the table
        $result = mysqli_query($this->db,$sql2);
        $user_data = mysqli_fetch_array($result);
        $count_row = $result->num_rows;

        if ($count_row == 1) {
            // this login var will use for the session thing
            $_SESSION['login'] = true; 
            $_SESSION['uid'] = $user_data['uid'];
            $_SESSION['ustore'] = $user_data['ustore'];
            return true;
        }
        else{
            return false;
        }
    }

    /*** for showing the username or fullname ***/
    public function get_fullname($uid){
        $sql3="SELECT fullname FROM users WHERE uid = $uid";
        $result = mysqli_query($this->db,$sql3);
        $user_data = mysqli_fetch_array($result);
        echo $user_data['fullname'];
    }

    /*** for showing the Store ID user ***/
    public function get_store($uid){
        $sql4="SELECT ustore FROM users WHERE uid = $uid";
        $result = mysqli_query($this->db,$sql4);
        $user_data = mysqli_fetch_array($result);
        echo $user_data['ustore'];
    }

    /*** for showing the NIK user ***/
    public function get_nik($uid){
        $sql5="SELECT unik FROM users WHERE uid = $uid";
        $result = mysqli_query($this->db,$sql5);
        $user_data = mysqli_fetch_array($result);
        echo $user_data['unik'];
    }

    /*** for showing the NAMA TOKO user ***/
    public function get_store_name($uid){
        $sql6="SELECT STORE_ID, STORE_NAME FROM users INNER JOIN store ON store.STORE_ID=users.ustore where uid = $uid ";
        $result = mysqli_query($this->db,$sql6);
        $user_data = mysqli_fetch_array($result);
        echo $user_data['STORE_NAME'];
    }

    /*** starting the session ***/
    public function get_session(){    
        return $_SESSION['login'];
    }

    public function user_logout() {
        $_SESSION['login'] = FALSE;
        session_destroy();
    }
}

class Store{

    public $db;
    public function __construct(){
        $this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

        if(mysqli_connect_errno()) {     
            echo "Error: Could not connect to database.";    
            exit; 
        }
    }

    /*** for showing the username or fullname ***/
    public function get_store_info($ustore){
        $sql="SELECT * FROM data_exp INNER JOIN store ON store.STORE_ID=data_exp.STORE ";
        $result = mysqli_query($this->db,$sql);
        $user_data = mysqli_fetch_array($result);
        echo $result;
    }
}

如何创建请求数据的函数($user_data['ustore'])。当我调用函数(<? php $store_id = $user->get_store ($uid); echo $ store_id;?>)时,它不会出现。任何人都可以帮助我吗?

我是PHP的初学者。

告诉我如何从用户处制作功能请求数据。

1 个答案:

答案 0 :(得分:-1)

试试这个

$user = new User;
$store_id = $user->get_store($uid);
print_r($store_id);