How to create a unique php page for each row in mysql database

I have a mysql database related to php. However, I want each row in the database to be a unique page. Can anyone provide some overview of how this will work. Itโ€™s easy for me to show the database results as complete tables or selected rows, but itโ€™s hard for me to create separate pages from unique rows.

+5
source share
3 answers

Well, at first you cannot, or it will be difficult to create separate pages for each row of your table. You will need to do this with one page and use the global $ _GET to change the site you should be browsing.

for instance site.php?id=1

site.php :

<?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'];
?>

, , /.

+9

, .

$sql = 'SELECT `content` FROM `articles` WHERE `id` = ' . (int) $_GET['id'];
+5

, URL- .

, id: 123 URL-, /search/id/123. $app->Property->search($array) MySQL. script JSON, Ajax JavaScript . JSON, PHP/HTML PHP .

<?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 along with a .htaccessscript that redirects all requests to my file index.php. Index.php at some point loads this file controller.php, which processes 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>
+1
source

All Articles