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 would work. It is simple for me to display the results of the database as a complete table or selected rows, but I have difficulty in creating individual pages from unique rows.
3 Answers
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.
2 Comments
http://stackoverflow.com/.../.../how-do-...-in-a-mysql-database. And it doesn't have a ?id=1questions/5175588/. The URL parser ignores the other text (which is left in for Search-engine ranking) and reads 'questions' and '5175588' to understand that it must load a question with ID/Row ID: 5175588Here 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>