0
<?php
        
session_start();
$link = mysqli_connect("localhost", "xxx", "xxxxxx", "xxx");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$id = $_GET['id'];
if ($result = mysqli_query($link, "SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6")) {
    while($row = mysqli_fetch_array($result))
    {
        $idS = $row['SWho'];
        echo $idS;
        if ($result = mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6")) {
            while($row = mysqli_fetch_array($result))
            {
                echo "<img src='Member_ProfilePics/";
                echo $row['PP'] . '.' . $row['Ext'];
                echo "' width=42 height=40 style='float: left'>";
            }
        }
    }
    //<img src='Member_ProfilePics/john.jpg' width=42 height=40 style='float: left'>
}                               
?>

Here in my code, I want to output the list of STo. and connect to other database and output the following rows in each STo.

Sample to be output:

(picture of id=1) 1 
(picture of id=2) 2 
(picture of id=3) 3 
(picture of id=4) 4 
(picture of id=5) 5 
(picture of id=6) 6

How? my code is not looping.

Sample output of my code:

(picture of id=1) 1
3
  • SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6" , how many entries does this query return ? Commented May 23, 2013 at 13:24
  • what's the output of mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6") Commented May 23, 2013 at 13:26
  • You shouldn't be doing nested trips to the database anyhow. Use a JOIN and make one trip. Commented Sep 10, 2024 at 3:13

1 Answer 1

2

You are using the same variables for both the loops:

if ($result = mysqli_query($link, "SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6")) {
    while($row = mysqli_fetch_array($result)) {

        ....

        if ($result = mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6")) {
            while($row = mysqli_fetch_array($result)) {
                ...
            }
        }
    }
} 

Try to change the seconda value to these

if ($result = mysqli_query($link, "SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6")) {
    while($row = mysqli_fetch_array($result)) {

        ....

        if ($result2 = mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6")) {
            while($row2 = mysqli_fetch_array($result2)) {
                ...
            }
        }
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.