• 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
  On 22/02/2013 at 06:51, virtorio said:

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.

  • 0
  On 22/02/2013 at 09:17, Mike said:

no no no no

I simply stated it as a way of getting the code to "work" without having to rewrite it; it was far from a recommendation.

This topic is now closed to further replies.
  • Posts

    • Gemini CLI brings AI smarts to your Firebase terminal experience by Paul Hill Google has added its Gemini AI model directly into the command-line interface of Firebase Studio, its cloud-based IDE that uses AI to help with projects. The Gemini Command Line Interface (CLI) means that developers can expand past using AI for code, they can now also use AI for content generation and research without leaving the IDE. Gemini CLI comes with free usage tiers (60 model requests per minute, 1,000 requests per day with a Google login), it offers advanced AI features, and includes integrated Google Search for real-time content. Gemini CLI is also open source so it can be customized and accepts contributions. Accessing the Gemini CLI within Firebase Studio is straightforward, just press “Code view” in the top-right. From there, open up the terminal from the burger menu then select Terminal and New Terminal. Then in the terminal, type gemini and go through the setup, you can just press enter twice to get started. Out of the box, you’ll be able to get started with Gemini 2.5 Pro by just typing a query and pressing enter. There is also a non-interactive mode that’s useful for scripting and automation. To use it you use the –prompt or -p flags followed by your query wrapped in quotes, for example: gemini -p “What is the capital of France?” In this mode, Gemini CLI automatically closes after completing the request. During setup, there was the option to choose a theme. If you ever want to change it or look at other settings such as usage states, tool access, or checkpointing, you can edit them via .gemini/settings.json. You can also add API keys or choose different models in .env and you can using GEMINI.md to provide project-specific context, instructions, and coding styles in Gemini for a more tailored response. With Gemini CLI, you can have it explain code, refactor code, debug errors, and summarize information. It’s as simple as typing explain [file], refactor , debug “Error message”, or summarize “topic”. There are also built-in commands for managing the session such as /help for a command list, /chat to save and resume conversations, /tools to see available tools, and /restore to undo tool-made file edits. Firebase Studio, in true Google fashion, is a cloud-based IDE used in your web browser, making it excellent for weaker computers. To get started, you can head to the Firebase website. From there, tap Studio in the top-right.
    • I would prefer local ai over online ai on some tasks . They are good enough for tasks like artificial voice , image editing , text corrections , tagging etc . Local AI on Windows Photo editor is impressive for example . There are probably many other Ai that we could run on simple pc with GPUs .
    • oh look, we are under the control of the U.S again, is it not about time we as a country did this ourselves if they want it, not rely on greedy U.S. tech companies? We are capable of doing so as a country. I hate how the U.K have gone with no industry like we used to have and relying on other countries. Maybe we should follow Trump words, and make Britain great again and stop bending over to please Trump and other countries. Our government is Trumps puppet. I know we need to co-operate on some things, but come on.
    • Actually I see no problem is notifying readers in advance. The post author mentioned that it'll be available on July 22.
    • We can actually disable this new AI powered tab groups suggestion feature. https://www.askvg.com/enable-o...mart-tab-groups-in-firefox/
  • Recent Achievements

    • Week One Done
      MIghty Haul earned a badge
      Week One Done
    • One Month Later
      MIghty Haul earned a badge
      One Month Later
    • Collaborator
      KD2004 earned a badge
      Collaborator
    • One Month Later
      ataho31016 earned a badge
      One Month Later
    • One Month Later
      Delahenty Machinery earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      593
    2. 2
      Michael Scrip
      201
    3. 3
      ATLien_0
      192
    4. 4
      +FloatingFatMan
      140
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!