Hey, all

I've got this script that runs a simple 'select' query:

function set_selectAssoc($usernumber) {
        $this->execute = $this->connection->query("SELECT * FROM `link-UsersToGroups` WHERE userID = ".$usernumber);
        $this->num_rows = $this->execute->num_rows;
        $this->results = $this->execute->fetch_assoc();

        if ($this->num_rows == 0) {
            $this->response_code = 2;
            $this->response_msg .= "You don't currently seem to belong to any groups yet.  Would you like to create one?";
        }
        else {
            $this->response_code = 0;
            echo "Number of rows found: ".$this->num_rows."<BR>";
            do {
                echo $this->row["groupID"]."<BR>";
            } while ($this->row = $this->results);
        }
    }

the first echo statement (echo "Number of rows found: ".$this->num_rows."<BR>";) returns correctly (two rows found), but, when it comes to the 'do...while' block that's supposed to walk through the results, it just echos the groupID of the first row in an endless loop.

Any suggestions?

Thank you!

-Jay

Thanks!

Member Avatar for diafol

You have this:

do {
    echo $this->row["groupID"]."<BR>";
} while ($this->row = $this->results);

Instead, try:

foreach($this->results as $row) echo $row["groupID"]."<BR>";

BTW - is this part of a class, as you're using $this all over the place.

Just another thing you may be at risk from SQL injection as you seem to be inserting unsanitized ipnut directly into the SQL statement. If this is the case, then use prepared statements instead and bind this parameter properly.

A good suggestion, but now it just walks through all the fields of the first record returned, along with the error "Warning: Illegal string offset 'groupID' in /home/roadtostage/roadtostage.com/includes/groupSummary.php on line 35" after each echo.

The actual return was:

Warning: Illegal string offset 'groupID' in /home/roadtostage/roadtostage.com/includes/groupSummary.php on line 35
4

Warning: Illegal string offset 'groupID' in /home/roadtostage/roadtostage.com/includes/groupSummary.php on line 35
1

Warning: Illegal string offset 'groupID' in /home/roadtostage/roadtostage.com/includes/groupSummary.php on line 35
5

Warning: Illegal string offset 'groupID' in /home/roadtostage/roadtostage.com/includes/groupSummary.php on line 35
D

Warning: Illegal string offset 'groupID' in /home/roadtostage/roadtostage.com/includes/groupSummary.php on line 35
P

on the other questions, this is part of a class.

This code in this class should be okay, because it's not reliant upon any user input (the values in the class all come from session data that's already determined, with no input fromt he user...

...unless there's an aspect of SQL injection attacks that I didn't take under consideration?

Member Avatar for diafol

Ah hold on, you need to loop on the fetch_assoc:

while($row = $this->execute->fetch_assoc()) echo $row["groupID"]."<br />";

Getting closer....it should be returning:

5
6

Now, instead of looping the groupID of the first record over and over again, it's now skipping the first record, and returning only the groupID of the second record:

6

Member Avatar for diafol

That's because you've still got the

$this->results = $this->execute->fetch_assoc();

line on Line 4 of your original post. Remove it.

That was what did it. Thank you for your assistance!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.