LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-16-2011, 02:23 AM   #1
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 98

Rep: Reputation: 16
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)
 
Old 04-18-2011, 12:43 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 3,666

Rep: Reputation: 204Reputation: 204Reputation: 204
Not an answer to your question, since, of course, fetching a row must change the internal pointers. But, for the output you want, I'd suggest just using the pstree command. (See man pstree for details.)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
qpopper mysql "sql query fetch row failed ()" hunterpoint Linux - Server 0 09-15-2010 08:44 AM
yum install php-mysql fails with mysql 5.1 - "Error: mysql conflicts with MySQL" rebelde Linux - Software 2 03-13-2009 10:32 AM
which is better "mysql" or "mysqli" to use in php 5.2.6 !? robertjinx Linux - Server 1 12-07-2008 08:32 AM
"set bell-style none" won't stop my internal speaker from beeping Lokathor Debian 7 11-26-2005 01:52 PM
curl "incompatible pointer type" while making PHP basse- Linux - Software 2 04-15-2004 10:21 AM


All times are GMT -5. The time now is 06:46 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration