470,855 Members | 1,301 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

mysql_data_see for for() loops?

Am I right to say mysql_data_seek allows me to walk through a database
return as I would an array?
for ($1=0; $i < $num; $i++) {
mysql_data_see($result, $i);
$row = mysql_fetch_array($results);
}

I still haven't solved the problem I mentioned in another post (I can
only get the first row back from my returns), so I'm tempted to go
this route.
Jul 17 '05 #1
4 1607
lawrence wrote:
Am I right to say mysql_data_seek allows me to walk through a database
return as I would an array? Why???
for ($1=0; $i < $num; $i++) {
mysql_data_see($result, $i);
$row = mysql_fetch_array($results);
}


I suppose you're looking for something like this?

while($row = mysql_fetch_assoc($res))
{
// your code which does anything with $row
}

Just rtfm a little bit...
--> http://php.net/mysql_fetch_array
(Three examples doing this.)
Jul 17 '05 #2
On 2004-03-05, lawrence wrote:
Am I right to say mysql_data_seek allows me to walk through a database
return as I would an array?
for ($1=0; $i < $num; $i++) {
mysql_data_see($result, $i);
$row = mysql_fetch_array($results);
}

I still haven't solved the problem I mentioned in another post (I can
only get the first row back from my returns), so I'm tempted to go
this route.


mysql_data_seek lets you move the array pointer to the specified point.
Why not just use:

while ( $row = mysql_fetch_array($results)) {
// Do your stuff
}

?

--
Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com
Jul 17 '05 #3
Mike Peters <o0****************@THIShotmail.com> wrote in message news:<1d******************************@news.terane ws.com>...
mysql_data_seek lets you move the array pointer to the specified point.
Why not just use:

while ( $row = mysql_fetch_array($results)) {
// Do your stuff
}


Because that would mean using msyql specific commands in the main part
of my code. I've masked out all the mysql stuff. I've a selectObject
that all queries get run through. It might be getting data from mysql
or postgre or a file or an xml stream. The code doesn't know where the
data comes from. So my loop looks like this:
$selectObject->setQuery("GetAllWeblogEntries");
$selectObject->getData();
$howMany = $selectObject->getCount();

for ($i=0; $i < $howMany; $i) {
$row = $selectObject->getNextRow();
}

This was working perfectly till this week, when I made some changes
and busted it. I'm not sure what I did wrong. The getCount method
still works correctly and returns the correct number of items that
have been returned. But then in the for() loop, the getNextRow()
method only works the first time. After that it returns nothing. I'm
very confused by this - I'd think either the pointer would advance to
the next row, or it would stay stuck on the first row and return that
for how ever many times $howMany equals. But instead it works once and
then returns nothing. However, it remains a valid resource pointing to
a database return. I know this because inside of getNextRow the result
pointer gets tested every time with is_resource($results) and it never
tests false. So I'm confused about what is going on.

Today, in desperate attempt to get the code working again, I changed
things so that I could pass the loop index into getNextRow and feed it
to mysql_data_seek. But it still didn't work. This is what I'm
currently trying:
$selectObject->setQuery("GetAllWeblogEntries");
$selectObject->getData();
$howMany = $selectObject->getCount();

for ($i=0; $i < $howMany; $i) {
$row = $selectObject->getNextRow($i);
}


then, inside getNextRow(), I go:

if (is_resource($result)) {
$success = mysql_data_seek($result, $i);
if ($success) {
$resultsObject->debugNotes("Tested true");
} else {
$resultsObject->addToErrorResults("Tested false");
}
}

And it keeps testing false. I'm a little stunned by this. Why is
mysql_data_seek() failing if the resource is testing true in
is_resource()? I'm having a damned hard time figuring it out.

I also echo out the actual query to the screen and then run it through
phpMyAdmin so I can see what kind of return I should expect. All day
I've been running my tests on one query where I know I should be
getting back 3 entries. phpMyAdmin tells me so and the method
getCount() correctly comes back with 3. And, as I said, the first row
comes back fine. After that nothing works.

I'm passing the resource pointer by reference, so what happens inside
the object should effect what happens outside.
Jul 17 '05 #4
lawrence wrote:
This was working perfectly till this week, when I made some changes
and busted it. I'm not sure what I did wrong.
Since I discovered CVS I wonder how I was able to do anything at all
without using it :-)

There's a more recent thing called "SVN" (if I remember correctly)
which I haven't yet tried out.
[...]
then, inside getNextRow(), I go:

if (is_resource($result)) {
$success = mysql_data_seek($result, $i);
if ($success) {
$resultsObject->debugNotes("Tested true");
} else {
$resultsObject->addToErrorResults("Tested false");
}
}

And it keeps testing false. I'm a little stunned by this. Why is
mysql_data_seek() failing if the resource is testing true in
is_resource()? I'm having a damned hard time figuring it out.
Test the return value from all mysql_* functions with mysql_error().

$success = mysql_data_seek($result, $i);
if ($success) {
$resultsObject->debugNotes("Tested true");
} else {
$resultsObject->addToErrorResults("Tested false with " . mysql_error());
}
I also echo out the actual query to the screen and then run it through
phpMyAdmin so I can see what kind of return I should expect. All day
I've been running my tests on one query where I know I should be
getting back 3 entries. phpMyAdmin tells me so and the method
getCount() correctly comes back with 3. And, as I said, the first row
comes back fine. After that nothing works.


Do you have error_reporting set to show all errors, warnings and
notices?
I have php.ini configured to *not* show any errors and save them to a
error log. However, when I'm having trouble with a script I do a

define('DEBUG', '1');

before requiring my standard include file, which has

if (DEBUG) {
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('error_log', '');
}

I find it much easier to see the errors in the browser than having to
open the error log.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Oleg Leschov | last post: by
15 posts views Thread by JustSomeGuy | last post: by
4 posts views Thread by Dr. David Kirkby | last post: by
6 posts views Thread by Scott Brady Drummonds | last post: by
17 posts views Thread by John Salerno | last post: by
10 posts views Thread by Putty | last post: by
2 posts views Thread by bitong | last post: by
3 posts views Thread by monomaniac21 | last post: by
8 posts views Thread by Nathan Sokalski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.