简体   繁体   中英

Store MySQL query result into a two-dimensional array

I have not seen a similar question so I'm asking a new one. I'm also experimenting with using loops and arrays, so apologies if this is a really dumb question or really bad code. I was able to do achieve my desired result with about 50 more lines of code, but it was even more inelegant (I basically had each query in a hardcoded line, instead of using variable). Hence, I am trying to learn how to code this a bit more neatly to make it more efficient. I might be going down the wrong path though...

I have a table with the following basic structure:

[id / username / email / content / side / category / rating]

Side can either be "yes" or "no" and Category can either be "red", "blue" or "green". These are restricted in the submission form.

I'm trying to execute the following query in a loop to pull all the required data and store them into a two-dimensional variable in php:

$dialecticSides = array("yes","no");
$numberSides = 2;
$dialecticRow = array("red","blue","green");
$numberRows = 3;

$dialectic_queryString = $dialectic_mysqlQuery = $dialectic_sqlResult = $dialectic_totalRows = array();

for($x=0;$x<$numberSides;$x++) {
    for ($y=0;$y<$numberRows;$y++) {        
        $dialectic_queryString[$x][$y] = "SELECT * FROM ". $dialectic_sqlTable ." WHERE side = '" . $dialecticSides[$x]. "' AND category = '". $dialecticRow[$y]."' ORDER BY rating DESC";
        $dialectic_mysqlQuery[$x][$y] = mysql_query($dialectic_queryString[$x][$y], $commenting_conn) or die(mysql_error());
        $dialectic_sqlResult[$x][$y] = mysql_fetch_assoc($dialectic_mysqlQuery[$x][$y]);
        $dialectic_totalRows[$x][$y] = mysql_num_rows($dialectic_sqlResult[$x][$y]);

        var_dump($dialectic_sqlResult);
    }
}

My desired output is something like this:

Array
(
    [0] => Array
        (
          [0] => Array
             (
               all the comments which are in row 0 (yes) and side 0 (red)
             )
          [1] => Array
             (
               all the comments which are in row 0 (yes) and side 1 (blue)
             )
          [2] => Array
             (
               all the comments which are in row 0 (yes) and side 2 (green)
             )
        )
    [1] => Array
        (
          [0] => Array
             (
                all the comments which are in row 1 (no) and side 0 (red)
             )
          [1] => Array
             (
               all comments which are in row 1 (no) and side 1 (blue)
             )
          [2] => Array
             (
               all the comments which are in row 1 (no) and side 2 (green)
             )
        )
)

Essentially, if I want to know if what I'm doing makes any sense at all or if I should give up and go back to hardcoding the queries to different result variables.

My query is failing - the output of the query is null. What am I doing wrong? Thanks in advance!

==== SOLVED it ====

for($x=0;$x<$numberSides;$x++) {
for ($y=0;$y<$numberRows;$y++) {        
    $queryString[$x][$y] = "SELECT * FROM comments WHERE topic_id = '{$topicid}' AND side = '{$x}' AND row = '{$y}'";
    $result[$x][$y] = mysqli_query($db_connection,$queryString[$x][$y]) or die(mysqli_error($db_connection));
    while($eachcomment = mysqli_fetch_assoc($result[$x][$y])) {
        $array[$x][$y][] = $eachcomment;
    }
}

You don't want to put your queries etc. into arrays.
You want just one query.
Define your 2 arrays to get your output rows and columns:

$rowarray = array("yes" => 0, "no" => 1); // 2 rows?
$colarray = array("red" => 0, "blue" => 1, "green" => 2); // 3 cols?

Initialize your output array:

$outputarray = array(array('', '', ''), array('', '', ''));

Get $result from your query.

SELECT * FROM table

(Don't have a WHERE clause)
Then process the results:

while (($qrow = mysql_fetch_assoc($result))<>NULL) {

$qrow is an associative array: 'side' => 'yes', 'category' => 'red', etc.
For each qrow, get the output row and col:

$row = $rowarray[$qrow['side']];
$col = $colarray[$qrow['category']]

At this point you can accumulate what you want into $outputarray:

$outputarray[$row][$col] .= $qrow['comments']; 

This is the end of your "while" loop:

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM