使用当前目录名查询MySQL表

时间:2012-08-26 05:29:05

标签: php mysql

是否可以使用当前目录名称(与现有表条目匹配)查询数据库?我正在尝试创建一个模板页面,该页面将根据它所在的当前目录提取内容。

所以代码的外观和结果会是这样的:

mysql_select_db($table);
$stud_query = "SELECT * from [table] WHERE name = [current url directory];
$result = mysql_fetch_assoc(mysql_query($stud_query));

因此,如果网址为mysite.com/stack/,则查询将返回结果,如果:

$stud_query = "SELECT * from [table] WHERE name ="stack";

2 个答案:

答案 0 :(得分:2)

你真的应该使用mod_rewrite并通过将url爆炸成片段来将完整路由传递给索引以进行处理/路由。

RewriteEngine On
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

示例:

<?php 
// http://example.com/controller/action/sub_action

if(isset($_GET['route'])){
    $url_parts = explode('/', $_GET['route']);
    $controller = (isset($url_parts[0]) ? $url_parts[0] : null);
    $action     = (isset($url_parts[1]) ? $url_parts[1] : null);
    $sub_action = (isset($url_parts[2]) ? $url_parts[2] : null);
}

//With this in mind think:
// http://example.com/user/lcherone
// http://example.com/user/logout
// http://example.com/admin/page/add
// Life just got a whole lot easier!!!
?>

答案 1 :(得分:0)

$curDir = array_shift(explode('/', substr($_SERVER['REQUEST_URI'],1)));
$stud_query = sprintf("SELECT * from %s WHERE name = '%s';", 'table', mysql_real_escape_string($curDir));