Adam.VT
Adam.VT

Reputation: 1

Query MySQL table using current directory name

Is it possible to query the database using the current directory name (which is matched to an existing table entry)? I'm trying to create a template page that will pull content based on the current directory it is in.

So the code would look and result something like this:

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

So if the url is mysite.com/stack/, the query would return results as if:

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

Upvotes: 0

Views: 257

Answers (2)

Lawrence Cherone
Lawrence Cherone

Reputation: 46610

Really you should use mod_rewrite and pass the full route to index for processing/routing by exploding the url into pieces.

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

Example:

<?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!!!
?>

Upvotes: 2

TheHe
TheHe

Reputation: 2972

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

Upvotes: 0

Related Questions