how do i create a unique php page for each row in a mysql database

后端 未结 3 1012
孤城傲影
孤城傲影 2021-01-02 21:56

I have a mysql database connected with php. However, I want each row in the database to be a unique page. Could anyone please provide some sort of overview for how this woul

相关标签:
3条回答
  • 2021-01-02 22:24

    Alright, first off you can't, or it will be difficult to create individual pages for each row of your table. You'll have to do it with one page and take use of the $_GET global to change which site you should view.

    For instance site.php?id=1

    site.php would look something like this roughly

    <?php
    $connect = mysql_connect('host', 'user', 'pass');
    $select_db = mysql_select_db('database_name');
    
    $id = mysql_real_escape_string($_GET['id']);
    //Remove LIMIT 1 to show/do this to all results.
    $query = 'SELECT `content` FROM `pages` WHERE `id` = '.$id.' LIMIT 1';
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    
    // Echo page content
    echo $row['content'];
    ?>
    

    With that you can request any id you want and retrieve the content of that row/page.

    0 讨论(0)
  • 2021-01-02 22:32

    Put an id in your query string which matches the id in your database.

    $sql = 'SELECT `content` FROM `articles` WHERE `id` = ' . (int) $_GET['id'];
    
    0 讨论(0)
  • 2021-01-02 22:47

    Here is an example of how I am taking a URL to display a different page for each row in a Property database.

    I've shown my search mechanism below, but it can take a URL such as /search/id/123 to display property with id:123. $app->Property->search($array) is where the MySQL code is executed. This script exports a JSON object, which is loaded by Ajax and displayed by JavaScript on the other end. You don't have to export JSON, you could also load a PHP/HTML template and export the page in PHP too.

    <?php
    /*
     * Controller
     * Decodes query string and takes appropriate action
     */
    
    $query = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
    $qs = array();
    foreach ($query as $key=>$value) $qs[$key] = urldecode(htmlspecialchars($value));
    array_shift($qs); #clear blank first element
    
    # Parse URLs with the following format:
    # /search/{$field1}/{$value1}/{$field2}/{$value2}/.../{$fieldN}/{$valueN}
    switch ($val = array_shift($qs)) {
    
        case "search":
            $params = array(); # query array
            $field = true; # switch between field or value
            while (!is_null($val=array_shift($qs))) {
                if ($field) $params[-1] = $val;
                else $params[$params[-1]] = urldecode($val);
                $field = !$field;
            }
            unset($params[-1]);
            $result = $app->Property->search($params);
            header('Content-type: application/json');
            echo json_encode($result, JSON_FORCE_OBJECT);
            exit; #don't load template after exporting JSON object
            break;
    ...
    

    This script works together with a .htaccess script that redirects all requests to my index.php file. The index.php at some point loads this controller.php file, which handles the URLs and loads the page depending on the URL.

    Here is the .htaccess code:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    0 讨论(0)
提交回复
热议问题