...

View Full Version : retrieve record fron db one by one



shajiuddin
01-21-2012, 08:06 AM
Hi,

I need to retrieve records one by one

for example: If I have nth numbers of records in db table then these nth numbers records I can get one by one using for loop or else then I can create a file for each record

Just help me about how can I fetch record one by one. the file creation process I can do my self

Regards

DarkLaika
01-21-2012, 11:15 AM
Hi,

I need to retrieve records one by one

for example: If I have nth numbers of records in db table then these nth numbers records I can get one by one using for loop or else then I can create a file for each record

Just help me about how can I fetch record one by one. the file creation process I can do my self

Regards

What are the columns in said table, and where would you want them to appear on the page?

First things first, you'd need to apply an id to each row in your table, if you haven't already, you may need to copy the rows from your table and start again, this time with a column called "id" before all the other columns. Give it a type of "INT" and an index of "PRIMARY", and turn on AUTO_INCREMENT. Now put all your rows back in, but leave the id column blank as this will update itself automatically.

Now create a new file, name it something like entry.php, and add the following code:


<?php
session_start();

//Replace "username" and "password" with your username and password for the FTP.
//If needed, change localhost to your appropriate server, but this is normally not necessary.
$connection = mysql_connect('localhost','username','password') or die('cannot connect to database');

//Replace database with your database name.
$database = mysql_select_db("database") or die('not selected');
?>
<html>
<head>
<title>test</title>
</head>
</html>
<body>
<?php
if(isset($_GET['entry']))
{
$entry = mysql_real_escape_string($_GET['entry']);
//Replace table with your table name.
$getEntry = mysql_query("SELECT * FROM table WHERE id = '$entry'");
?>
<?php
while($entry_data = mysql_fetch_array($getEntry))
{
?>
<?=$entry_data['id']?>
<br />
<?=$entry_data['name']?>
<?php
}
}
?>
</body>
</html>

That code would display the id for the entry and the name of the entry. You can change <?=$entry_data['column']?> to display any information related to the row that has been fetched, just replace what's between the apostrophes with the column name and it will display it.

Now to view this page the user would have to go to yoursite.com/entry.php?entry=*id*

If you want to change this to something like yoursite.com/entry/1/ you can edit the .htaccess file which should be located in the root directory of your domain. If it's not already there, create it. Add the following code:


RewriteEngine on

RewriteRule ^entry/([A-Za-z0-9]+)/$ /entry.php?entry=$1

If the "RewriteEngine on" is already there, you just need to add this code below it:


RewriteRule ^entry/([A-Za-z0-9]+)/$ /entry.php?entry=$1

So in the end you can just type yoursite.com/entry/1/ and it will show you any data for a row that's got the id 1, and so on so forth.

Hope this helps.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum