CodeCharge Studio
search Register Login  

Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> General/Other

 How to reset pointer to database?

Print topic Send  topic

Author Message
CodeChargenewbie
Posted: 03/25/2008, 8:58 AM

CodeCharge provides classes for talking to the database, with such functions as
next_record() and close(). So, normally, I traverse through the result of a
query using next_record(). Say my database query returns 10 records and I
traverse through all 10 records. At this very point, is there a way I can reset
the pointer to the first record (or any record of my choosing, for that matter)
without querying the database again? It's stupid to query the database twice
for the same request just reset its data pointer to the first row. There must
be some way in Codecharge to reset the data pointer...

Important: I'm using PHP and codecharge 4.0 and the latest patch (0.04).

Thank you.
---------------------------------------
Sent from YesSoftware forum
http://forums.codecharge.com/
wkempees


Posts: 1679
Posted: 03/25/2008, 4:37 PM

$DB<Connectionname>->seek();
seek() would (re)position to row 0, default value.
$DB<Connectionname>->seek(1), position to row 1.

AFAIK seek() should return row 0, being the first row in the result sets array.

Have a go and report back your findings.
How to find out: open db_mysql.php and have a look at the content.

Walter

_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
CodeChargenewbie

Posts: 114
Posted: 03/26/2008, 6:48 AM

thank you so much.

Unfortunately, it failed to work. I want to make it clear I tried it after completely exhuasted the query results. In other words, I ran a while loop on next_record(), and after the while loop completed, then I began testing.

$dbcon->seek() did not work, as I got a missing argument error. $dbon->seek(0), for example, produced no errors but nothing happened appeared to be happen; I typed $dbcon->f("FieldName") right after and it didn't print anything.

NOW...I decided have a go inside the while loop. In this particular query result, I have 30 rows. I created a counter outside the while loop that, when it hits 5, initiates seek(0) and prints a fieldname INSIDE the while loop. So it looks something like this:

$counter = 0;

while($dbcon->next_record()
{
...
if($counter == 5) {
$dbcon->seek(0);
print $dbcon->f("FieldName");
$counter++;
}

What will happen is it'll get to row 5 in the while loop and do its thing. Then it reaches the if statement (at this point, counter is 5) and executes the code inside. It resets the pointer to the first row (row 0). HOWEVER, it prints the FieldName from row 5! Why? I don't know. I tried inserting $dbcon->next_record(); before the print statement. What happened? Well, it printed nothing and the while loop resetted to row 1 (as opposed to resetting to row 0 when the if statement was printing FieldName from row 5).

Then I tried changing seek(0) to seek(10) (I took out the $dbcon->next_record() part). The while loop processed rows 0-5, skips 6, 7, 8, and 9, and starts again at row 10. Unfortunately it didn't print the FieldName. Inserting $dbcon->next_record() again just caused the while loop to skip 6, 7, 8, 9, and 10, and start at row 11. Once again, it didn't print FieldName.

I wish codecharge would put a manual or something to explain how exactly to use every single class and function and when and where they're appropriate. It's like leading the blind into the middle of heavy traffic or the equivalent of writing code without any comments!

If anybody can figure out how this seek function works, please say something.

View profile  Send private message
wkempees


Posts: 1679
Posted: 03/26/2008, 11:39 AM

Does not work, sorry.
Seems Seek was not implemented fully.
Will try workaround for you, may take some time, due to flue.
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
wkempees


Posts: 1679
Posted: 03/26/2008, 3:53 PM

I have located the way this works.

$dbcon->seek(nn);

will reset the pointer to the rownumber indicated by 'nn'

$dbcon->next_record()

will fetch the row 'nn'+1

So
$dbcon->seek(15);
$dbcon->next_record()
will fetch row 16 and $dbcon->f('FieldName') will hold the result for row 16.

BUT:
If your while($dbcon->next_record()) loop processes all the rows, then the resultset will be freed, therefore seek() will not position to any row.
Two ways to make your requirement work:
1: break out of the loop BEFORE all rows are processed
2: in db_mysql.php find 'Auto_Free' and set that to 0, default = 1

Method 2 however might have bad implications for the rest of your application, you may have to do a $dbcon->free_result() yourself.
Method 1 is doable in your example

Walter




_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
wkempees


Posts: 1679
Posted: 03/26/2008, 4:28 PM

from Examples in Help file

  
$db = new clsDB<connection_name>();  
$db->query("SELECT article_id,article_title FROM articles");  
$articles = array();  
while ($db->next_record())  
{  
	$a = array();  
	$a["article_id"] = $db->f("article_id");  
	$a["article_title"] = $db->f("article_title");  
	$articles[] = $a;  
}  

What happens here is that your dataset is traversed, results stored in an arry.
You could then position within the array any row you like..........
Would mean you have to do no changes to any common files, and utilize the examples given from within CCS.
(Yes good examples are in there)

Walter
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
szenczi

Posts: 67
Posted: 03/27/2008, 6:26 AM

My problem is joining with this discussion:
How can I point (and change) the field value in the empty row(s) of an editable grid?

http://forums.yessoftware.com/posts.php?post_id=95342

Thanks,
Norbert
_________________
CCS5.1, PhP, MySQL
View profile  Send private message
wkempees


Posts: 1679
Posted: 03/27/2008, 6:46 AM

@Norbert
wrong post I assume, please do not change the subject.
Walter
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
CodeChargenewbie

Posts: 114
Posted: 03/27/2008, 8:02 AM

Quote wkempees:
I have located the way this works.

$dbcon->seek(nn);

will reset the pointer to the rownumber indicated by 'nn'

$dbcon->next_record()

will fetch the row 'nn'+1

So
$dbcon->seek(15);
$dbcon->next_record()
will fetch row 16 and $dbcon->f('FieldName') will hold the result for row 16.

BUT:
If your while($dbcon->next_record()) loop processes all the rows, then the resultset will be freed, therefore seek() will not position to any row.
Two ways to make your requirement work:
1: break out of the loop BEFORE all rows are processed
2: in db_mysql.php find 'Auto_Free' and set that to 0, default = 1

Method 2 however might have bad implications for the rest of your application, you may have to do a $dbcon->free_result() yourself.
Method 1 is doable in your example

Walter





First, coincidently, the while loop I'm using is very similar to the example you displayed, except mine serves a purpose other than holding the results of a query to traverse indefinitely.

Second, it may be a crucial detail, but I'm employing MSSQL, as opposed to MYSQL. Method 2 is still a possibility in my case, though.

$dbcon->seek(15);  
$dbcon->next_record()  
//will fetch row 16 and $dbcon->f('FieldName') will hold the result for row 16.  

Again, I documented this above that this failed to work for me -- nothing appeared when I printed the FieldName.[Edit: Please disregard failed to print. I don't know why it didn't print prior, but I redid the example in my code, it printed so go figure.] However, without next_record() immediately following seek(), it printed the FieldName at the position prior to position 15. Like in my example,

  
$counter = 0;  
while($dbcon->next_record())  
{  
...  
  
if($counter ==2) {  
$dbcon->seek(5);  
print $dbcon->f("FieldName");  
}  
$counter++;  
}  

, the while loop will process row 0, 1, and 2. When it arrives at seek, however, seek tells the pointer to move to row 5 but prints FieldName in row 2. The if statement ends, counter is increased by 1, and the while loop begins again. And the while loop does process the query result after this point. Now, if I do this,

  
...  
if($counter ==2) {  
$dbcon->seek(5);  
$dbcon->next_record();  
print $dbcon->f("FieldName");  
}  
...  
, it'll print FieldName from row 5, even though I moved the pointer to the next record! Then, when the while loop begins again, this time, it starts processing at row 6.

Now take a look at this one:

  
$counter = 0;  
while($dbcon->next_record())  
{  
...  
print $dbcon->("FieldName");  
...  
if($counter ==2) {  
$dbcon->seek(5);  
print $dbcon->f("FieldName");  
}  
$counter++;  
}  

What's strange is once the while loop starts over again, after the if statement is entered of course, the first print statement displays FieldName from row 5. So, based on what you said, seek does not move the database pointer. It merely instructs the database pointer to prepare for the "move" instruction (in this case, $dbcon->next_record()) by specifying the position.

...
I just re-read your post again and it makes sense. And I want to tell you I appreciate your help and finding the answer.

I should profess isn't there a way to use php sql functions to move the database pointer, even after breaking out of the while loop, and without changing the Auto_Free variable to 0? Maybe one must make database calls completely in php sql to accomplish such a feat, because I can't change the Auto_Free variable and I need that while loop to complete fully. The array is a great idea, though. But maybe there's another way by employing php's sql functions either solely or combining it with codecharge's sql functions...
View profile  Send private message
CodeChargenewbie

Posts: 114
Posted: 03/27/2008, 8:02 AM

It appears there may be a way.

I created a variable called $result and assigned $dbcon->query(...) to it. I then initiated a var_dump on $result and it displayed "resource(30) of type (mssql result)" (not sure what a resource ID of 30 means). Maybe I can leverage this and call the Free_Result function (http://us.php.net/manual/en/function.mssql-free-result.php) to turn it off just for this query, I don't know...
View profile  Send private message
CodeChargenewbie

Posts: 114
Posted: 03/27/2008, 8:02 AM

sorry again.
View profile  Send private message
CodeChargenewbie

Posts: 114
Posted: 03/27/2008, 8:02 AM

again, sorry (blame it on safari for windows)
View profile  Send private message
wkempees


Posts: 1679
Posted: 03/27/2008, 9:15 AM

Be aware:
Using the first examples you are using the DBLIB, mssql / mysql version.
These are wrapped SQL statements in a library (object) manner making live easier.
Nothing stops you using native SQL handling, as long as you understand and write out the full statements.
The $result way can be made to work but needs another approach.
read up on it and decide yourself wether that path is your path.

Consider this topic solve, for now?
Walter
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Internet Database

Visually create Web enabled database applications in minutes.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.