• 0

Mysql error help


Question

G'day All,

First off, if this is in the wrong forum, could a mod move it to the correct one.

I was bored and thought I'd have a go at some php. I have no real idea what I'm doing but thought I'd start learning from a script I already had.

In this function, the line mysql_data_seek($sql,$row) throws up the following error.

Warning: mysql_data_seek(): Offset 2 is invalid for MySQL result index 8 (or the query data is unbuffered)


function traverse($root, $depth, $sql)
{
$row=0;
while ($acat = mysql_fetch_array($sql))
{
if ($acat['parentcat'] == $root)
{
echo "<option value='" . $acat['catID'] . "'>";
$j=0;
while ($j<$depth)
{
echo "  ";
$j++;
}
if($depth>0)
{
echo "-";
}
echo $acat['catname'] . "</option>";

mysql_data_seek($sql,0);
traverse($acat['catID'], $depth+1,$sql);
}
$row++;
mysql_data_seek($sql,$row);
}
}
[/CODE]

Any help on this would be appreciated.

Link to comment
https://www.neowin.net/forum/topic/1137886-mysql-error-help/
Share on other sites

8 answers to this question

Recommended Posts

  • 0

I've never used mysql_data_seek before (and neither should you because it's depreciated (as are all mysql_xxx functions) and will disappear in a future version of PHP, instead use mysqli or better yet PDO), but at a guess I'd say you're receiving this warning because you're trying to seek to a row that doesn't exist in the result.

You may not have seen that warning before if the code has been used in a production server because warnings are usually turned off so users don't see them.

You could use mysql_num_rows to get the number of rows in the result and only call mysql_data_seek if it's not outside the number of rows available.

function traverse($root, $depth, $sql)
{
  $num_rows = mysql_num_rows($sql);
  ...
  if ($row &lt; $num_rows)
	mysql_data_seek($sql,$row);

You could also call mysql_data_seek with at @ infront of it to suppress the error message.

$row++;
@mysql_data_seek($sql,$row);

  • 0

I've never used mysql_data_seek before (and neither should you because it's depreciated (as are all mysql_xxx functions) and will disappear in a future version of PHP, instead use mysqli or better yet PDO), but at a guess I'd say you're receiving this warning because you're trying to seek to a row that doesn't exist in the result.

You may not have seen that warning before if the code has been used in a production server because warnings are usually turned off so users don't see them.

You could use mysql_num_rows to get the number of rows in the result and only call mysql_data_seek if it's not outside the number of rows available.

You're right Virtorio, the script is from about 2006 I think. :D

As I said, I have no real idea of what I'm doing and after a few searches came to the conclusion that the error is generated for the reason you stated.

I did try and use mysql_num_rows, but couldn't work out how to put it in the correct place.

Where would be a good tutorial to rewrite the function in the correct manner?

Thank you for the quick reply as well. :)

  • 0

So, this is looping over a set of records that correspond to a hierarchical data structure stored using the Adjacency Model, printing them out in an indented fashion.

All records are gone through, looking for direct children of a given node, which for each one identified is printed and the function recalled to print its own direct children and so on. Each printing of a nodes children must loop through the entire result set from scratch looking for them, hence why the pointer is set back to 0 prior to calling the function to get its children. After getting its children printed, it needs to reset the pointer to the next record it is supposed to be moving on to. Here is where there's a flaw.

It's incrementing the variable that keeps track of where the pointer should be pointing to, so that it should be pointing to the next record, and then setting the pointer to it. However, upon having processed the last record in the set, the pointer is incremented to one greater than the index of the last item (itself) and therefore trying to set the pointer to it fails. A simple off by one bug.

All you should need to do is switch the order of the last two statements. Set the pointer to the current record, then increment $row. If mysql_fetch_array works how I think it works (I've never messed with setting the index pointer myself), the call to it will itself increment its pointer and fetch the next row, if there is one.

Furthermore, I'd set default values on the root and depth parameters if I were you, it'll make it simpler to use, by just calling traverse($sql);

function traverse($sql, $root = null, $depth = 0)
{
	$row = 0;
	while ($acat = mysql_fetch_array($sql))
	{
		if ($acat['parentcat'] == $root)
		{
			echo "&lt;option value='" . $acat['catID'] . "'&gt;";
			$j = 0;
			while ($j &lt; $depth) {
				echo "  ";
				$j++;
			}
			 if ($depth &gt; 0) {
				echo "-";
			}
			echo $acat['catname'] . "&lt;/option&gt;";

			mysql_data_seek($sql, 0);
			traverse($sql, $acat['catID'], $depth + 1);
		}
		mysql_data_seek($sql, $row);
		$row++;
	}
}

  • 0

Thank you to both Virtorio and +theblazingangel.

Tried what you suggested +theblazingangel, got rid of that error but threw up a different one. :)

Did what you suggested Virtorio and that worked a treat.

Thank you both very much.

This topic is now closed to further replies.
  • Posts

    • Dragon's Dogma 2: Dark Arisen expansion to bring snowy region, new updates also coming by Pulasthi Ariyasinghe Capcom had a surprise waiting for Dragon's Dogma fans today in the Nintendo Direct presentation. The company revealed an expansion for the second installment with a name that should be familiar to series veterans. Coming later this year, Dragon's Dogma 2: Dark Arisen is promising a massive new region to explore, new monsters, fresh skills to learn, and more. The studio says players will be heading to the Northern region of the world, named Norgan, to find new secrets about an undying "Fallen Dragon." There will be forgotten relics that the protagonist can find to unlock fresh weapons and skills the expansion is introducing. Players will also be able to find mysterious equipment from a previous Arisen as a part of the expansion, all part of 12 Lost Rites Dungeon Challenges they must complete to gain access. In Neowin's own review, I found Dragon's Dogma 2 to be an impressive RPG when it launched back in 2024, giving the title an 8.5/10 for its class variants, companion system, and immersive exploration. "Once a prosperous region of the kingdom of Vermund, it was abandoned many years ago for reasons unknown," says Capcom about the new region. "Long has it been since any soul traveled its paths. Blanketed in heavy snow, these frigid lands are home to savage hordes and creatures of unbelievable power. Those who are capable of vanquishing such fearsome foes, or those who possess a keen eye for exploration, will find themselves rewarded with powerful relics." Dragon’s Dogma 2: Dark Arisen expansion launches on October 9, 2026, with a $29.99 price tag. Ahead of the expansion release, Capcom is also planning to release two free updates to the base game. The first will land tomorrow, June 10, bringing more accessible fast travel with an Eternal Ferrystone and other quality-of-life adjustments. The second update will land sometime in August, aiming to improve frame rates, add more save slots, and bring even more community-requested adjustments. This expanded Dark Arisen edition is also launching on the Nintendo Switch 2 on the same day the content comes to PC, Xbox Series X|S, and PlayStation 5.
    • Classic themes are just the colors on the bar like the olden days, if you use the image themes, it does fancy transparent backgrounds and it makes the elements of the app look like they are transparent bubbles. This sample image shows what it looks like.  
    • Good point, unfortunately. NextDNS has far more filters and workarounds than uBlock, and it's easy to implement.
    • Windows 10 KB5094127 Patch Tuesday improves File Explorer search and more by Taras Buria The June 2026 Patch Tuesday updates are here, bringing mandatory patches to users with PCs enrolled in the Extended Security Update program for Windows 10. Microsoft is rolling out KB5094127, with build numbers 19045.7417 and 19044.7417. Changelog includes the following: [File Explorer] This update improves File Explorer search, including support for Chinese text, and UTF 8–encoded files without a byte order mark (BOM). Text now displays more clearly and consistently across search results, Content view, and tooltips. [Secure Boot] This update enables dynamic status reporting for Secure Boot states in Windows Security App. This update adds a new policy setting, LimitSecureBootRequiredServiceData, under Computer Configuration > Administrative Templates > Windows Components > Secure Boot. When this setting is enabled, Windows limits the Secure Boot service data it sends by suppressing the event normally sent to Microsoft. This policy is also included in the Windows Restricted Traffic Limited Functionality Baseline package. For information about the policy, see Manage connections from Windows 10 and Windows 11 operating system components to Microsoft services. With this update, Windows quality updates include additional high confidence device targeting data, increasing coverage of devices eligible to automatically receive new Secure Boot certificates. Devices receive the new certificates only after demonstrating sufficient successful update signals, maintaining a controlled and phased rollout. As for known bugs, Microsoft has the following to say: A workaround is available in the official documentation. Today's updates are available for PCs enrolled in the Extended Security Updates program only. If your PC is eligible, you can download the update from Settings > Windows Update or from the Microsoft Update Catalog here.
    • Then the solution is to not let children have easy access to smart phones or internet until they are older, not mass surveillance. Only this would require parents to do actual parenting, most likely, as with any good solution to the problem.
  • Recent Achievements

    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
    • One Year In
      jojodbn earned a badge
      One Year In
    • One Month Later
      jojodbn earned a badge
      One Month Later
    • Week One Done
      jojodbn earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      523
    2. 2
      PsYcHoKiLLa
      231
    3. 3
      +Edouard
      124
    4. 4
      ATLien_0
      87
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!