php stop "internal row pointer" when fetch from mysql


 
Thread Tools Search this Thread
Top Forums Programming php stop "internal row pointer" when fetch from mysql
# 1  
Old 04-16-2011
php stop "internal row pointer" when fetch from mysql

Hi gurus, I need to call php function for fetching data from mysql but I dont want to move internal row pointer (IRP) forward. Or even get the actual IRP position then fetch data and move IRP back to remembered position. Function mysql_data_seek() is not suitable for my example because I am trying to fetch data recursive here is code (See the pseudocode commented section):

Code:
<?php

include_once("connection.php");
$tree="";
$depth=1;
$tempTree="";

// THIS RETURNS THE ROOTS FROM WHICH I WILL BEGIN
$sql_query="SELECT DISTINCT(ppid) FROM process WHERE (ppid) NOT IN (SELECT pid from process);";
$nav_query = MYSQL_QUERY($sql_query);

WHILE ( $nav_row = MYSQL_FETCH_ARRAY($nav_query) )
{
    //echo "calling build_child() from while with argument: " . $nav_row['ppid'] . "<br>";
    $tree .= build_child($nav_row['ppid']);
}

FUNCTION build_child($ppID)
{
    GLOBAL $depth, $tempTree;
    //$depth = $GLOBALS['depth'];
    //$tempTree = $GLOBALS['tempTree'];

    /*
    PSEUDOCCODE THAT SHOULD BE PASTED HERE:
    1 FETCH ARRAY WITHOUT MOVING DATA POINTER AND STORE t_begin ADN t_end;
    2 MAKE NEW SELECT INSTEAD CURRENT WHICH WILL CONTAINT STORED VALUES FROM PREVIOUS AS CONDITION
    
    */

    // THIS SHOULD BE REPLACET WITH SOMETHING LIKE:
    // SELECT * FROM process WHERE ppid=2541 AND t_begin >= 1302865259 AND t_end <= 1302865269;
    // THE t_begin and t_end SHOULD COME FROM PREVIOUS SELECT WHICH DID NOT MOVE INTERNAL POINTER
    $sql_query = "SELECT * FROM process WHERE ppid=" . $ppID . ";";
    $nav_query = MYSQL_QUERY($sql_query);

    WHILE ( $nav_row = MYSQL_FETCH_ARRAY($nav_query) )
    {
        FOR ( $c=0;$c<$depth;$c++ )
        {
            //$tempTree .= "&nbsp;";
            $tempTree .= " - ";
        }
                
        $tempTree .= $nav_row['name'] . " (" . $nav_row['pid'] . ") <br>";
        //echo "tempTree: " . $tempTree;
        $depth++;
        //echo "depth: " . $depth . "<br>";
        
        //echo "calling build_child() from buildchild with argument: " . $nav_row['pid'] . "<br>";
        build_child($nav_row['pid']);        
    }
    $depth--;
    
    //echo "depth: " . $depth . "<br>";

    RETURN $tempTree;
}

echo "<br>";
echo $tree;



?>

For better understanding data in table looks like:
Code:
mysql> select * from process;
+----+------------+------------+------+------+------------------------------+
| id | t_begin    | t_end      | pid  | ppid | name                         |
+----+------------+------------+------+------+------------------------------+
|  1 | 1302865260 | 1302865269 | 2544 | 2541 | bash-new-child1-child1       |
|  2 | 1302865259 | 1302865269 | 2541 | 2510 | bash-new-child1              |
|  3 | 1302865261 | 1302865269 | 2542 | 2510 | bash-new-child2              |
|  4 | 1302800037 | 1302800049 | 2544 | 2541 | bash-old-child1-child1       |
|  5 | 1302800036 | 1302800049 | 2541 | 2510 | bash-old-child1                |
|  6 | 1302800038 | 1302800049 | 2542 | 2510 | bash-old-child2              |
|  7 | 1302866248 | 1302866250 | 2546 | 2545 | bash2-child1                 |
|  8 | 1302866248 | 1302866250 | 2547 | 2546 | bash2-child1-child1          |
|  9 | 1302866248 | 1302866250 | 2550 | 2545 | bash2-child2                 |
| 10 | 1302866248 | 1302866250 | 2551 | 2550 | bash2-child2-child1          |
+----+------------+------------+------+------+------------------------------+
10 rows in set (0.01 sec)

and at the output of php I would like something like ptree:

Code:
2510
 | - 2541 (bash-new-child1)
 |    |- 2544 (bash-new-child1-child1)
 |
 | - 2542 (bash-new-child2)


2510
 | - 2541 (bash-old-child1)
 |    |- 2544 (bash-old-child1-child1)
 |
 | - 2542 (bash-old-child2)

2545
 | - 2546 (bash2-child1)
 |    | - 2547 (bash2-child1-child1)
 |
 | - 2550 (bash2-child2)
    | - 2551 (bash2-child2-child1)

# 2  
Old 04-20-2011
If you are fetching using a bidirectional cursor, then you have to crawl back. If not, there is no going back. Maybe you should do more processing in a stored procedure inside mysql, where many quick adjustments are possible. Or, in a more formal language, where you can cache your rows in your choice of containers, and recall them at speed.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Solaris

Solaris 5.10 "Move Pointer" stuck

Hey there, I joined this forum just now cause I need help with an old SUN machine at work. I work on a helpdesk and we use a SOLARIS 5.10 OS. Every once in a while we will try to move one of our windows out of the way to make room on the desktop, and sometimes the mouse doesn't release what we... (7 Replies)
Discussion started by: TRex_2005
7 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. UNIX for Dummies Questions & Answers

Counting vowels in string. "Comparison pointer-integer".

I'm trying to write a programme which scans strings to find how many vowels they contain. I get an error saying that I'm trying to compare a pointer and an integer inif(*v == scanme){. How can I overcome this ? Also, the programme seems to scan only the first word of a string e.g.: if I type "abc... (1 Reply)
Discussion started by: fakuse
1 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Dummies Questions & Answers

Meaning of $var->{"@$row[0]"}=" "; ???

while (my $row = $sth->fetchrow_arrayref) { $var->{"@$row"}=" "; } Can anyone help me understanding above mentioned. i) As per my knowledge $row is taking ARRAY Refernce from the database ii) @$row is containing the value of 0th index of the array, testted the same. but I am not able... (0 Replies)
Discussion started by: jaigs_27
0 Replies

7. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

8. Programming

Fetch "my" IP address in HPUX 10.20

It sure sounds like a simple request.... I need some C code that will enable me to fetch the IP address of the tty attached to the current process. In short: char *myIPAddress(void) { // somebody help me fill in this part!! } -BL:confused: (4 Replies)
Discussion started by: brianleahy
4 Replies

9. Shell Programming and Scripting

awk: getline NOM < "-" script does not stop

Well , i have: $ cat example.dat 1 2 3 4 5 $ cat getline3.awk function getName (NOM) { printf "Enter a filename: " getline NOM < "-" # get response return NOM } BEGIN { printf ("\nREP: %s\n",getName(N)) } {print} This example works fine: (2 Replies)
Discussion started by: Klashxx
2 Replies
Login or Register to Ask a Question