dp152153 2017-11-03 10:14
浏览 25

根据网址从数据库中获取内容

I am trying to get the content from the database depending on the url. Lets say that you are on index.php then I want to get the content from the database with the entity page. My database looks like this:

|id | page | header | content | feature | footer |
|01 |index | header | content | feature | footer |

It is not the whole as it is a project that I can't show all of it, so I put in just dummy data in it here.

The config.php:

<?php
$url = $_SERVER['REQUEST_URI'];
$dbhost = "dummyhost";
$dbuser = "dummyuser";
$dbpass = "";
$dbname = "dummy";

$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

if(!$conn){
  die("ERROR");
}

$index = mysqli_query($conn, "SELECT FROM pages WHERE page LIKE '".$url."%');
$indexRow = mysqli_fetch_assoc($index);
$indexHeader = $indexRow['header'];
$indexContent = $indexRow['content'];
$indexFooter = $indexRow['footer'];
$indexfeature = $indexRow['feature'];

How can I do it? I know that I can use $_GET to get some variables from the adress, but in this case I am not using any variables to go to another site. Can I still use that or am I on the right track?

  • 写回答

1条回答 默认 最新

  • dpbyr64224 2017-11-03 13:44
    关注

    EDIT: As the question says NOT to use the query string.

    I am trying to get the content from the database depending on the url. Lets say that you are on index.php then I want to get the content from the database with the entity page.

    You can get the filename called by

    $path = parse_url($url, PHP_URL_PATH);
    $path = explode('&',$path);
    $filename = substr($path[0], -4); // removing '.php'
    

    You can create your SQL accordingly now and use the filename.

    评论

报告相同问题?