• 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

    • Universal USB Installer 2.0.3.6 by Razvan Serea The Universal USB Installer (UUI) is a powerful bootable USB software tool for creating USB boot drives from ISO files, perfect for installing Linux or Windows, running live systems, or building diagnostic toolkits. This versatile ISO-to-USB software makes it easy to boot from USB and create Live USBs for Linux distributions, Windows setup installers, antivirus tools, and system diagnostic utilities. Whether you need a multisystem Windows Media Creation Tool, a Live USB Linux installer, or an all-in-one PC diagnostic toolkit, UUI offers a reliable and flexible Linux and Windows bootable USB creator. Effortlessly carry your favorite portable operating systems and essential troubleshooting and diagnostic tools on a single flash drive or USB boot stick. Take your preferred Live Linux distributions, Windows installers, recovery software, backup utilities, and diagnostic tools with you, all bootable from a single USB drive. No more juggling multiple USB sticks or complicated bootloaders, UUI consolidates everything into one flexible, multiboot solution. Using this open source USB boot maker software is easy as 123. To create a Linux or Windows bootable USB drive, you simply select your target flash drive, choose your distribution from the list, browse to the ISO file (or choose to download the ISO), and then click Create. Once finished, you should have a ready to run Live USB containing the Live operating system, Windows installation media, or system diagnostics utility, or advanced system cleaner tool you previously selected. Universal USB Installer 2.0.3.6 fixes: Fixed volume label never being set after disk preparation. Fixed selected disk not being restored when returning to the drive selection page after preparation. Download: Universal USB Installer 2.0.3.6 | 19.4 MB (Open Source) Link: Universal USB Installer Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Browser vendors including Chrome, Firefox, Brave, and Vivaldi have jointly written an open letter to Microsoft saying enough is enough regarding Edge being pushed on Windows users without consent. Microsoft makes it difficult to change the default browser, promotes Edge throughout Windows 11, and has made the uninstall process unnecessarily complicated compared to other apps. Does anyone have more details on what exactly the open letter demands and whether any regulatory action is being considered? Also curious whether this could lead to antitrust investigations similar to what happened with Internet Explorer back in the day. Would love to hear from anyone following this closely.
    • Amen, they are a shady company... they modify my site to remove inline ads and then cry about subscribing to their services, a few things they "bravely" did in recent years: In 2020, Brave was caught automatically appending its own affiliate referral codes to URLs when users typed the web addresses of crypto exchanges (like Binance and Coinbase) into the address bar. Following significant backlash, the CEO apologized and the feature was disabled. In 2023, the company faced criticism after users noticed a paid VPN service was installed on their computers without their explicit consent. TOR Privacy Leak: In 2021, Brave's private Tor tabs were found to be leaking DNS queries to internet service providers, a flaw that was subsequently patched. Passive Cryptocurrency Donations: In early versions of the browser, Brave automatically collected user contributions and held them in a pool to be distributed to creators. If a creator had not signed up to claim their funds within a certain time frame, the process sparked criticism about where the money went.
    • I think they still believe it but are realizing that those statements created massive backlash and impacted adoption. I personally think AI, if used regularly will give you back about 30% of your time if you are an “information” worker.
  • Recent Achievements

    • One Month Later
      johnjacobb40 earned a badge
      One Month Later
    • One Year In
      Primer1st earned a badge
      One Year In
    • Experienced
      JayZJay went up a rank
      Experienced
    • Reacting Well
      Sir_Timbit earned a badge
      Reacting Well
    • Week One Done
      rubentuben8 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      PsYcHoKiLLa
      229
    3. 3
      +Edouard
      144
    4. 4
      ATLien_0
      86
    5. 5
      Steven P.
      82
  • Tell a friend

    Love Neowin? Tell a friend!