2013-10-26 174 views
0

我有一個域名,其中的用戶有一個子域名。從兩個表中獲取信息

實施例:

  • 用戶:維托爾
  • 子域:vitor.google.com

該數據庫具有列usermy_users和具有列birthdayaddresspasswordcitystate的表users_infos。 我該怎麼做在index.php的PHP中可以直接獲得這個信息的用戶到數據庫?

這是我的不正確的代碼:

// Get Subdomain 
$urlExplode = explode('.', $_SERVER['HTTP_HOST']); 
if (count($urlExplode) > 2 && $urlExplode[0] !== 'www') { 
    $subdomain = $urlExplode[0];  
    echo $subdomain; 
} 


// Select DB 
$sql = "SELECT * FROM users_infos i INNER JOIN my_users u on u.id = i.id where u.users='$users'"; 
    $result = mysql_query($sql); 
    if($result === FALSE) { 
    die(mysql_error()); 
    // TODO: better error handling 
    } 

else { 
$row = mysql_fetch_array($result); 
$userTitleSite = $row['userTitleSite']; 
echo $id_textos = $row["id_textos"]; 
echo "<br />"; 
echo $id = $row["id"]; 
echo "<br />"; 
echo $phone = $row["phone"]; 
echo "<br />"; 
echo "<br />"; 

} 

// Says that the subdomain is = user 
$subdomain = $user; 

正確答案如下:

使用你給我的幫助下,它看起來像我在這裏.. 遵循正確的代碼,我可以爲將來做什麼有同樣問題的用戶:

// Get subdomain 
$urlExplode = explode('.', $_SERVER['HTTP_HOST']); 
if (count($urlExplode) > 2 && $urlExplode[0] !== 'www') { 
    $subdomain = $urlExplode[0];  
    echo $subdomain; 
} 


// Says that the subdomain is = user 
$user = $subdomain; 


// Select DB 
$sql = "SELECT * FROM vms_textos i INNER JOIN vms_users u on u.id = i.id where u.user='$user'"; 
    $result = mysql_query($sql); 
    if($result === FALSE) { 
    die(mysql_error()); 
    // TODO: better error handling 
    } 

else { 
$row = mysql_fetch_array($result); 
$userTitleSite = $row['userTitleSite']; 
echo "<br />"; 
echo $id_textos = $row["id_textos"]; 
echo "<br />"; 
echo $user= $row["user"]; 
echo "<br />"; 
echo $id = $row["id"]; 
echo "<br />"; 
echo $telefone = $row["telefone"]; 
echo "<br />"; 
echo "<br />"; 

} 

echo "<br />"; 
echo "<br />"; 

謝謝大家的幫助! :)

+1

凡作爲SQL參數變量,$用戶,設置? – trf

+0

@trf我忘了添加在帖子中,但仍然不起作用 –

回答

0
$subdomain = $user; 

應該在$ sql之前,因爲您的代碼寫入$ user是空的。

+0

也不工作。 \ = –

+0

是$用戶一個單一的領域?你有沒有嘗試使用u.id進行查詢? – user2883341

0

您確定WHERE子句u.id = i.id正確嗎?換句話說,my_users和users_infos表中的ID列是否匹配?這是比較常見的有上users_infos表的外鍵引用(例如my_users_id),在這種情況下,查詢會是這樣的:

SELECT * 
FROM users_infos i 
INNER JOIN my_users u on u.id = i.my_users_id 
WHERE u.users='$users'