soycharliente Posted May 14, 2008 Share Posted May 14, 2008 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 More sharing options...
shedokan Posted May 14, 2008 Share Posted May 14, 2008 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++; } ?> Link to comment https://forums.phpfreaks.com/topic/105627-solved-returning-a-specific-row/#findComment-541152 Share on other sites More sharing options...
soycharliente Posted May 14, 2008 Author Share Posted May 14, 2008 Yeah. I thought about that. I just didn't know if there was a function or some extra parameter for one that could just return it by itself. Link to comment https://forums.phpfreaks.com/topic/105627-solved-returning-a-specific-row/#findComment-541154 Share on other sites More sharing options...
shlomikalfa Posted May 14, 2008 Share Posted May 14, 2008 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]; } Link to comment https://forums.phpfreaks.com/topic/105627-solved-returning-a-specific-row/#findComment-541158 Share on other sites More sharing options...
moselkady Posted May 14, 2008 Share Posted May 14, 2008 I am not sure, but this may work. Try: $sql = "SELECT * FROM data ORDER BY thedate DESC LIMIT 7,1"; Link to comment https://forums.phpfreaks.com/topic/105627-solved-returning-a-specific-row/#findComment-541160 Share on other sites More sharing options...
soycharliente Posted May 14, 2008 Author Share Posted May 14, 2008 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(); } ?> Link to comment https://forums.phpfreaks.com/topic/105627-solved-returning-a-specific-row/#findComment-541192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.