Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    160
    Thanks
    10
    Thanked 1 Time in 1 Post

    error in php code

    Hi

    I have been given this code and not sure thy when I run this script I get no output.

    Can anyone see whats wrong and how to correct please. Its the php code as I know the query works.


    PHP Code:
    <?php
    $hostname 
    "localhost";
    $database "sextoys";
    $username "sextoys";
    $password "sextoys";
    mysql_connect($hostname$username$password) or die(mysql_error());
    mysql_select_db($database) or die(mysql_error());
    $query="SELECT t1.title AS lev1, t1.sectionID AS id1,
            t2.title as lev2, t2.sectionID AS id2,
            t3.title as lev3, t3.sectionID AS id3,
            t4.title as lev4, t4.sectionID AS id4
    FROM jss_sections AS t1 
    LEFT JOIN jss_sections AS t2 ON t2.parent = t1.sectionID 
    LEFT JOIN jss_sections AS t3 ON t3.parent = t2.sectionID 
    LEFT JOIN jss_sections AS t4 ON t4.parent = t3.sectionID 
    WHERE t1.title = 'Main Section'"
    ;
    $id1 = -1
    while ( 
    get a row into $row )
    {
        if ( 
    $row['id1'] != $id1 )
        {
            echo 
    $row['lev1'] . "<br/>\n";
            
    $id2 = -1$id3 = -1$id4 = -1;
            
    $id1 $row['id1'];
        }
        if ( 
    is_set($row['id2'] && $row['id2'] != $id2 )
        {
            echo 
    $row['lev1'] . " >> " $row['lev2'] . "<br/>\n";
            
    $id3 = -1$id4 = -1;
            
    $id2 $row['id2'];
        }
        if ( 
    is_set($row['id3'] && $row['id3'] != $id3 )
        {
            echo 
    $row['lev1'] . " >> " $row['lev2'] . " >> " $row['lev3'] . "<br/>\n";
            
    $id4 = -1;
            
    $id3 $row['id3'];
        }
        if ( 
    is_set($row['id4'] && $row['id4'] != $id4 )
        {
            echo 
    $row['lev1'] . " >> " $row['lev2'] . " >> " $row['lev3'] . " >> " $row['lev4'] . "<br/>\n";
            
    $id4 $row['id4'];
        }
    }
    mysql_close();
    ?>
    Thanks
    Roy

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,288
    Thanks
    12
    Thanked 345 Times in 341 Posts
    Quote Originally Posted by ROYW1000 View Post
    PHP Code:
    while ( get a row into $row 
    this certainly is not valid PHP.

  • #3
    Regular Coder
    Join Date
    Jan 2010
    Posts
    160
    Thanks
    10
    Thanked 1 Time in 1 Post
    Hi

    Have tried removing that with no joy. Not sure if it needs the following or not

    $result=mysql_query($query);

    $num=mysql_numrows($result);

    and something like this:-

    $T=0;
    while ($T < $num) {

    $T1=mysql_result($result,$T,"T1");
    $T2=mysql_result($result,$T,"T2");
    $T3=mysql_result($result,$T,"T3");
    $T4=mysql_result($result,$T,"T4");
    ?>

    As had something like this in another query but its been ammended and not sure what its missing now.

    Thanks
    Roy

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,288
    Thanks
    12
    Thanked 345 Times in 341 Posts
    rather use mysql_fetch_row() and related, that’s the standard way to handle the result. (Although the PDO Guy would first fetch the primary keys and then get each row separately)

  • #5
    Regular Coder
    Join Date
    Jan 2010
    Posts
    160
    Thanks
    10
    Thanked 1 Time in 1 Post
    Hi

    I have tried your suggestion and run the script and all I am getting is a totally blank screen with no error messages.

    PHP Code:
    <?php 
    $hostname 
    "localhost"
    $database "sextoys"
    $username "sextoys"
    $password "sextoys"
    mysql_connect($hostname$username$password) or die(mysql_error()); 
    mysql_select_db($database) or die(mysql_error()); 
    $query="SELECT t1.title AS lev1, t1.sectionID AS id1, 
            t2.title as lev2, t2.sectionID AS id2, 
            t3.title as lev3, t3.sectionID AS id3, 
            t4.title as lev4, t4.sectionID AS id4 
    FROM jss_sections AS t1  
    LEFT JOIN jss_sections AS t2 ON t2.parent = t1.sectionID  
    LEFT JOIN jss_sections AS t3 ON t3.parent = t2.sectionID  
    LEFT JOIN jss_sections AS t4 ON t4.parent = t3.sectionID"
    ;
    $result=mysql_query($query);
    if (!
    $result) {
        echo 
    'Could not run query: ' mysql_error();
        exit;
    }
    $row mysql_fetch_row($result);
    mysql_close(); 
    $id1 = -1;  
    while

        if ( 
    $row['id1'] != $id1 
        { 
            echo 
    $row['lev1'] . "<br/>\n"
            
    $id2 = -1$id3 = -1$id4 = -1
            
    $id1 $row['id1']; 
        } 
        if ( 
    is_set($row['id2'] && $row['id2'] != $id2 
        { 
            echo 
    $row['lev1'] . " >> " $row['lev2'] . "<br/>\n"
            
    $id3 = -1$id4 = -1
            
    $id2 $row['id2']; 
        } 
        if ( 
    is_set($row['id3'] && $row['id3'] != $id3 
        { 
            echo 
    $row['lev1'] . " >> " $row['lev2'] . " >> " $row['lev3'] . "<br/>\n"
            
    $id4 = -1
            
    $id3 $row['id3']; 
        } 
        if ( 
    is_set($row['id4'] && $row['id4'] != $id4 
        { 
            echo 
    $row['lev1'] . " >> " $row['lev2'] . " >> " $row['lev3'] . " >> " $row['lev4'] . "<br/>\n"
            
    $id4 $row['id4']; 
        } 
    }
    mysql_close(); 
    ?>

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,288
    Thanks
    12
    Thanked 345 Times in 341 Posts
    may I point out that the manual page has sufficient examples as well as links worth further reading, which also have sufficient examples?

    I wonder that your code executes at all. (while usually has a loop condition, etc.)

  • #7
    Regular Coder
    Join Date
    Jan 2010
    Posts
    160
    Thanks
    10
    Thanked 1 Time in 1 Post
    Hi

    I have tried everything tonight to try and fix this all I know at the moment is that the issue is in the 2nd part of the script the 2nd if statement.

    PHP Code:
    $query="SELECT t1.title AS lev1, t1.sectionID AS id1,  
            t2.title as lev2, t2.sectionID AS id2,  
            t3.title as lev3, t3.sectionID AS id3,  
            t4.title as lev4, t4.sectionID AS id4  
    FROM jss_sections AS t1   
    LEFT JOIN jss_sections AS t2 ON t2.parent = t1.sectionID   
    LEFT JOIN jss_sections AS t3 ON t3.parent = t2.sectionID   
    LEFT JOIN jss_sections AS t4 ON t4.parent = t3.sectionID 
    where t1.title='Main Section'"
    ;
    $id1 = -1
    $result mysql_query($query) or die(mysql_error());
    while(
    $row mysql_fetch_array($result)){
    if ( 
    $row['id1'] != $id1 )  
    {  
    echo 
    $row['lev1'] . "<br/>\n";    
    $id2 = -1$id3 = -1$id4 = -1$id1 $row['id1'];
    }
    if (isset(
    $row['id2'] && $row['id2'] != $id2 )) 
        { 
            echo 
    $row['lev1'] . " >> " $row['lev2'] . "<br/>\n"
            
    $id3 = -1$id4 = -1
            
    $id2 $row['id2']; 
        } 
    }  

    mysql_close();
    ?> 
    If I stop the code at the 2nd if I get one result through which is correct for that level. The 2nd part of the script should bring out a further 80+ result but I just get a blank screen.

    I have tried DEBUG and get no errors, I have PHP error reporting on and it shows nothing wrong but all I get is this blank page.

    Any help please.

    Roy

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,288
    Thanks
    12
    Thanked 345 Times in 341 Posts
    not sure if this is intended
    PHP Code:
    if (isset($row['id2'] && $row['id2'] != $id2 )) 
    I’d expected rather
    PHP Code:
    if (isset($row['id2']) && $row['id2'] != $id2 
    although I’d say the isset() part is not necessary, since you put "id2" in through your SQL.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •