0

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

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

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";
4

2 回答 2

2

真的,您应该使用 mod_rewrite 并通过将 url 分解成碎片将完整的路由传递给 index 以进行处理/路由。

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!!!
?>
于 2012-08-26T05:46:24.543 回答
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));
于 2012-08-26T05:38:55.860 回答