1

Hi everyone and thanks for helping me out... I have 1 database and 3 queries at the same time. Every query is picking a different year and taking the data from that column.

This is my code

$items = array();

      // while we still have rows from the db, display them
while ($row = mysql_fetch_array($result1)) {

    $items[] = $row;
}

while ($row = mysql_fetch_array($result2)) {

    $items[] = $row;
}

while ($row = mysql_fetch_array($result3)) {

    $items[] = $row;
}


mysql_close($connect);

?>

<?php foreach($items as $key => $item) : ?>

    <?php print "I DONT KNOW WHAT TO DO"; ?>

<?php endforeach; ?>

When I type:

<?php echo print_r($keys); ?>

I can see that I have an array of 0, 1 and 2. I would love to get a specific ROW from the first(1) or second(2) array.

I am trying to figure it out and I just cant...

Thank you all so much!

Edit:

Complete code:

    <?php
    include ('db.php'); // for db details
    include ('functions.php');

    $connect = @mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>');
    if (!$connect) {
        die('Could not connect: ' . mysql_error());
    }



    @mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>');

    $query1 = "SELECT * FROM godina2015 WHERE mjesec='8' AND godina='2015'";
    $query2 = "SELECT * FROM godina2015 WHERE mjesec='7' AND godina='2015'";
    $query3 = "SELECT * FROM godina2015 WHERE mjesec='6' AND godina='2015'";

    $result1 = @mysql_query("$query1") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>');
    $result2 = @mysql_query("$query2") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>');
    $result3 = @mysql_query("$query3") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>');


    $items = array();

          // while we still have rows from the db, display them
    while ($row = mysql_fetch_array($result1)) {

        $items[] = $row;
    }

    while ($row = mysql_fetch_array($result2)) {

        $items[] = $row;
    }

    while ($row = mysql_fetch_array($result3)) {

        $items[] = $row;
    }


    mysql_close($connect);

    ?>

    <?php foreach($items as $key => $item) : ?>

        <?php print_r ($item); ?>

    <?php endforeach; ?>

EDIT #2

    <?php
    include ('db.php'); // for db details
    include ('functions.php');

    $connect = mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>');
    if (!$connect) {
        die('Could not connect: ' . mysql_error());
    }



    mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>');

    $query = "SELECT * FROM godina2015 WHERE mjesec IN(8,7,6) AND godina='2015'";

    $result = mysql_query("$query") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>');

    $items = array();

          // while we still have rows from the db, display them
    while ($row = mysql_fetch_array($result)) {

        $items[] = $row;
    }

    mysql_close($connect);

    ?>

    <?php foreach($items as $key => $item) : ?>

        <?php echo $items[1]['AWAsistCTR2']; ?>

    <?php endforeach; ?>
  • What if you do a `print_r($item);` in your foreach loop ? – Vincent G Apr 26 '16 at 15:50
  • this might help http://stackoverflow.com/questions/17139453/php-accessing-multidimensional-array-values – Jeff Puckett Apr 26 '16 at 15:50
  • show the `var_dump($items)`. you need to show/explain exactly what your data structure looks like. but given how you're building the array, you're going to end up with `$items[0]...$items[n]` for your first query results, `$items[n+1] ... $items[n+m]` for the second query, etc... – Marc B Apr 26 '16 at 15:51
  • @VincentG I get the list of all arrays: [0] => 82 [id] => 82 [1] => 8 [mjesec] => 8 [2] => 2015.... so on – Mario Semeš Apr 26 '16 at 15:52
  • You might like to start by showing us the ACTUAL queries you are running. Who knows maybe you are doing something silly in them – RiggsFolly Apr 26 '16 at 15:53
  • Show us the queries, It looks like you are running 3 totally different queries and expecting to be able to process the output from all 3 the same way. – RiggsFolly Apr 26 '16 at 15:55
  • **Also:** Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Apr 26 '16 at 15:56
  • @RiggsFolly complete TEST code added – Mario Semeš Apr 26 '16 at 15:57
  • Also if you used `mysql_fetch_assoc()` you would only get one reference to each column, instead of an assoc reference and a numberic reference as well – RiggsFolly Apr 26 '16 at 15:59
  • Also stop using `@` sign Error suppression. **Got an error.... Fix it** – RiggsFolly Apr 26 '16 at 16:00
  • Why not do one query `SELECT * FROM godina2015 WHERE mjesec IN(8,7,6) AND godina='2015' ORDER BY mjesec DESC` ??? – AbraCadaver Apr 26 '16 at 16:00
  • You beat me to it by MILLI SECONDS @AbraCadaver – RiggsFolly Apr 26 '16 at 16:01
  • It would be hard to find one script with more errors/bad coding practices. Yu really should look for a few good books/tutorials. If the book/tutorial uses `mysql_*` functions **throw it aways** and look for another that uses PDO – RiggsFolly Apr 26 '16 at 16:03

2 Answers2

0

Inside the loop do a var_dump($item).

You will see the structure of the array/object in your browser.

Look for the field that has the data you want (it will probably have the same name as the column in your database).

You can access it by using $item['field'] inside the loop. Or $items[array number here(0,1,2)]['field'].

And I believe when you said ROW you meant COLUMN.

Paulo Arromba
  • 108
  • 1
  • 12
  • We could help you better if, instead of the code you just added you did a var_dump($items) after the mysql_close($connect) and pasted the output here. – Paulo Arromba Apr 26 '16 at 16:05
0

My first answer ... so excited. I kind of know what you are trying to do, as I have to do the same thing sometimes when I am using the code to "figure out" how the different (dynamic) query responses relate to each other. I generally nest the results so that I have something relational that can be logically parsed.

while ($row = mysql_fetch_array($result1)) {
    $items[0][] = $row;
}

while ($row = mysql_fetch_array($result2)) {
    $items[1][] = $row;
}

while ($row = mysql_fetch_array($result3)) {
    $items[2][] = $row;
}

This would distinguish the different recordsets so that you could parse the array in whatever manner you wanted, even recursively if needed.

(array)$newArray = parseArray($items);

Thence ...

function parseArray($inArray) {
    (array)$outArray = Array();

    foreach($inArray as $element) {
        if('some condition with $element') {        //maybe $element['date']=10?
            $outArray[] = parseArray($element);
        } else {
            $outArray[] = $element;
        }
    }
    return $outArray();
}

Hard to answer (and test) without knowing what data looks like, but maybe some ideas. I now brace for my first down-vote.

bt3of4
  • 36
  • 6