2

I query and fetch and array from a mysql table. When I print_r($array) I get the below output. What does it mean when the word "Array" are outputted several times. Does it mean I get 5 arrays or does it mean I have one array? - I am confused, please explain.

My php code

<?php
$results = mysql_query("SELECT * FROM task_list");
    while($array = mysql_fetch_array($results)) {    
        print_r($array);
    }
?>

My table info:

CREATE TABLE `task_list` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title_task_DK` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8;

Print_r output:

Array
(
    [0] => 1
    [id] => 1
    [1] => Dæk For
    [title_task_DK] => Dæk For
)
Array
(
    [0] => 2
    [id] => 2
    [1] => Dæk Bag
    [title_task_DK] => Dæk Bag
)
Array
(
    [0] => 3
    [id] => 3
    [1] => Dæk Alm.
    [title_task_DK] => Dæk Alm.
)
Array
(
    [0] => 4
    [id] => 4
    [1] => Dæk Indl.
    [title_task_DK] => Dæk Indl.
)
Array
(
    [0] => 5
    [id] => 5
    [1] => Slange/Lapning For
    [title_task_DK] => Slange/Lapning For
)
Nomis
  • 437
  • 1
  • 4
  • 13
  • You aren't saying how you're getting $array. – Andrew Nov 18 '14 at 17:57
  • 3
    You could show your actual PHP code. The while/foreach is more likely to explain it. – mario Nov 18 '14 at 17:57
  • @mario and I updated and added my php code. – Nomis Nov 18 '14 at 18:00
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – esqew Nov 18 '14 at 18:03
  • @esqew thanks I am aware of that, this is just for me and to be changed. – Nomis Nov 18 '14 at 18:04
  • Well, someone else can probably explain that more understandably: but you're essentially polling multiple arrays from the DB, then `print_r`-dump them individually. // While you're new and don't have invested in much code, you should immediately switch to [`PDO`](http://php.net/manual/en/book.pdo.php) (don't fall for `mysqli`!), which provides a much simpler interface. (In this case a `foreach` or just using `->fetchAll` would make an interesting comparison.) – mario Nov 18 '14 at 18:04
  • @mario. good point. I hope the print_r isn't in the loop. Looks like in this case it may be. – unixmiah Nov 18 '14 at 18:17
  • Your question obviously brought up some questions people don't fully understand. So I'm +1 this in hopes that others can look at it and get a better understanding. You do have everything in correctly as long as you're looking to know what is in each row that you are returning. – Cayce K Nov 18 '14 at 18:26

2 Answers2

0

This might not be the perfect answer to the question, but it worked for me.

I ended up changing the PHP script so the print_r output was more readable and in one array.

New PHP script:

<?php
$results = mysql_query("SELECT * FROM task_list");
$value = array();
while($row = mysql_fetch_array($results)) {    
 $value[] = $row['title_task_DK'];
}
 echo('<pre>');
 print_r($value);
 echo('</pre>');
?>

The new PHP script gave me this:

Array
(
    [0] => Dæk For
    [1] => Dæk Bag
    [2] => Dæk Alm.
    [3] => Dæk Indl.
    [4] => Slange/Lapning For
)

Thank you all for commenting and answering my question.

Ps. that said, please read: Why shouldn't I use mysql_* functions in PHP?.

Community
  • 1
  • 1
Nomis
  • 437
  • 1
  • 4
  • 13
-1

This is what you have from print_r($your_array_from_mysql):

Array
(
    [0] => 1
    [id] => 1
    [1] => Dæk For
    [title_task_DK] => Dæk For
)
Array
(
    [0] => 2
    [id] => 2
    [1] => Dæk Bag
    [title_task_DK] => Dæk Bag
)
Array
(
    [0] => 3
    [id] => 3
    [1] => Dæk Alm.
    [title_task_DK] => Dæk Alm.
)
Array
(
    [0] => 4
    [id] => 4
    [1] => Dæk Indl.
    [title_task_DK] => Dæk Indl.
)
Array
(
    [0] => 5
    [id] => 5
    [1] => Slange/Lapning For
    [title_task_DK] => Slange/Lapning For
)

It's one single array. If you assign a variable to this array you can access it by the array key. You can also echo it out.

$array = $array_from_mysql['key'];

Think of the print_r function as a diagnostic function. It shows everything that's in a array.

You only have one single array but it prints it that way. You can also grab fields straight from MySQL with fetch_assoc or fetch_array; it will form the array differently. You can also access db fields as objects from mysql to take it to the next level.

unixmiah
  • 3,081
  • 1
  • 12
  • 26
  • 2
    it isn't 1 array it is each and every row that is in the fetch array from the mysql statement. He does in this instance have 5 different arrays, but in reality he has 5 different rows. – Cayce K Nov 18 '14 at 18:07
  • we need to see the code. @nomis, can you post your code? – unixmiah Nov 18 '14 at 18:09
  • .. the amount of code that is shown is sufficient to see that he is asking why he is showing 5 arrays. See my answer. – Cayce K Nov 18 '14 at 18:10
  • "You only have one single array but it prints it that way". Not correct (well, I'm not sure if I get what you mean). He's most likely doing a loop. He has one array, and he's doing a print_r() inside that loop (or has five arrays, but that sounds more unlikely). There's no other way for a print_r to output that. – alxgb Nov 18 '14 at 18:13
  • ...@unixmiah I would appreciate if you (based on my scenario) could give an example of your suggestion "_access db fields as objects from mysql to take it to the next level_". – Nomis Nov 19 '14 at 12:20
  • @Nomis You're doing it right based on your code. You're print_r statement was in the wrong place. Make sure you have $array = mysql_fetch_array($results); then do a print_r($array) after mysql_fetch without the while loop to understand what's in the fetch_array from the DB. Not to worry the confusing part yesterday was why the print_r was in the while loop; that's why you saw 5 Arrays being printed because you had 5 records in your DB and the while loop was going at it 5 times echoing 5 print_r statements. You can set it up in different ways to understand it. – unixmiah Nov 19 '14 at 16:29
  • Thank you @unixmiah. I really appreciate your effort and answers. I just added my own answer. – Nomis Nov 20 '14 at 13:35