Jump to content

[SOLVED] Returning a specific row


soycharliente

Recommended Posts

Example:

<?php
// what entry to get
$id = $_GET['id'];

// 10 most recent entries
$sql = "SELECT * FROM data ORDER BY thedate DESC LIMIT 10";
?>

 

$id is just a number 1-10. It has no association with any numbers in the database. If $id is 7, I want the 7th result from the query. Is it possible to return that row?

Link to comment
https://forums.phpfreaks.com/topic/105627-solved-returning-a-specific-row/
Share on other sites

try this:

<?php
// what entry to get
$id = $_GET['id'];

// 10 most recent entries
$sql = "SELECT * FROM data ORDER BY thedate DESC LIMIT 10";

$result = mysql_query($sql) or die(mysql_error());

$i=1;
while($row = mysql_fetch_array($result)){
    if($i==7){
echo $row['TheFieldName'];
    }
$i++;
}
?>

hi, i'm using this little UDF (User defined function], just call it with the right vars.

//$Table - the table u'r working on.
//$Variable - What row are you looking for ?
//$WhereVar - What row do you want to use for where?
//$WhereIs - What should be in the row u want to use as where.
function GetSingleVarFromTable($Table, $Variable, $WhereVar, $WhereIs = "")
{
if ($WhereIs == "")
{ 
	$query = "SELECT `$Variable` FROM `$Table` where `$WhereVar` limit 1";
}else{
	$query = "SELECT `$Variable` FROM `$Table` where `$WhereVar`='$WhereIs' limit 1";
}
$result = mysql_query($query);
   if (!$result){return FALSE;}
   $ActivationCodeAr = mysql_fetch_array($result);
return $ActivationCodeAr[$Variable];
}

A friend told me about a function called mysql_data_seek().

 

Here's the code that I am using.

 

<?php
$id = $_GET['id'];
if (isset($id))
{
dbconnect();
$sql = "SELECT * FROM `data` ORDER BY `thedate` DESC LIMIT 10";
$result = mysql_query($sql) OR DIE ("{$sql}<br />".mysql_error());
if (mysql_num_rows($result) > 0)
{
	mysql_data_seek($result, $id - 1);
	$r = mysql_fetch_assoc($result);
	echo "{$r['one']}<br />{$r['two']}<br />{$r['three']}";
}
dbclose();
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.