2016-09-22 52 views
-1

這是一個腳本,用於在PHP中以echo的形式檢索用戶數據。爲什麼我在php函數中獲取商店名稱數據

<?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的初學者。

告訴我如何向用戶發送功能請求數據。

+0

它應該是'回聲$ STORE_ID;'?這個錯誤說的是什麼? –

回答

-1

試試這個

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

不工作,這不顯示:( –

+0

我編輯過,你可以再試一次,你在哪裏粘貼這個代碼,並且還假設$ uid是已知的。 –

相關問題