我有一個域名,其中的用戶有一個子域名。從兩個表中獲取信息
實施例:
- 用戶:維托爾
- 子域:vitor.google.com
該數據庫具有列user
表my_users
和具有列birthday
,address
,password
,city
和state
的表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 />";
謝謝大家的幫助! :)
凡作爲SQL參數變量,$用戶,設置? – trf
@trf我忘了添加在帖子中,但仍然不起作用 –