Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

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.

like image 602
berkcss Avatar asked Mar 03 '11 01:03

berkcss


2 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.

like image 159
Henrik Skogmo Avatar answered Oct 10 '22 00:10

Henrik Skogmo


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

$sql = 'SELECT `content` FROM `articles` WHERE `id` = ' . (int) $_GET['id'];
like image 43
alex Avatar answered Oct 10 '22 01:10

alex