使用当前目录名查询MySQL表


Query MySQL table using current directory name

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

所以代码看起来和结果是这样的:

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

因此,如果url是mysite.com/stack/,那么查询将返回如下结果:

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

实际上,您应该使用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!!!
?>
$curDir = array_shift(explode('/', substr($_SERVER['REQUEST_URI'],1)));
$stud_query = sprintf("SELECT * from %s WHERE name = '%s';", 'table', mysql_real_escape_string($curDir));