0

Context: I have a large table that I'm trying to assign to several variables. My columns are named things like "grp" or "scr". There are 48 rows, so to isolate each item, I want to be able to call the variables "grp1" or "scr5" or "grp48".


getdata.php

$m = 1;
while ($m<=48) {
    $conn->query("SELECT * matches WHERE mid = '$m'");
    while($row = mysqli_fetch_array($result)) {
        $grp(1-48) = $row['grp']; // Not sure how to approach this.
    }
    $m++;
}

Any help is appreciated!

2
  • 3
    What is wrong with using an array here? Commented May 17, 2014 at 16:27
  • I'm not opposed to other methods of handling this. Could you elaborate? Commented May 17, 2014 at 16:28

1 Answer 1

2

Use an array. If you don't know how to use an array, read a book on PHP.

Here's one: http://oreilly.com/catalog/progphp/chapter/ch05.html

Also, don't run 48 queries where 1 will be more efficient.

$conn->query("SELECT * matches WHERE mid BETWEEN 1 AND 48 ORDER BY mid");
while($row = mysqli_fetch_array($result)) {
    $grp[] = $row['grp']; // add each element to the end of the $grp array
}

After this is done, you'll have an array $grp with 48 entries. You can read each element individually:

echo $grp[24];
Sign up to request clarification or add additional context in comments.

2 Comments

@JamesPatrick Beware that you'll have an index shift as php arrays start at 0 and you read your entries from 1
@kingkero, good tip, that's covered in the chapter I linked to.

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.