Jump to content

mysqli_fetch_array - issue?


Drongo_III

Recommended Posts

Hi Guys

 

Using mysqli_fetch_array(), which i had anticipated would simply return a numeric array with all the results from the simple query. However, it keeps coming out as a multidimensional array, which isn;t what i want.

 

Is this just how it works or have i done something wrong?

 

Code

 

$mysqli = new mysqli("localhost", "root", "", "ik");
		if ($mysqli->connect_errno) {
			echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
		}



	$qry = "SELECT amount FROM teams";
	$result = $mysqli->query($qry);

	while($row = mysqli_fetch_array($result, MYSQLI_NUM))
		{
		$rows[] = $row;
		//echo $row . "<br/>";
		}

		print_r($rows);

 

 

Result

 

Array
(
    [0] => Array
        (
            [0] => 3
        )

    [1] => Array
        (
            [0] => 25
        )

    [2] => Array
        (
            [0] => 26
        )

    [3] => Array
        (
            [0] => 31
        )

    [4] => Array
        (
            [0] => 34
        )

    [5] => Array
        (
            [0] => 44
        )

    [6] => Array
        (
            [0] => 50
        )

    [7] => Array
        (
            [0] => 56
        )

    [8] => Array
        (
            [0] => 65
        )

    [9] => Array
        (
            [0] => 221
        )

    [10] => Array
        (
            [0] => 222
        )

    [11] => Array
        (
            [0] => 225
        )

    [12] => Array
        (
            [0] => 2210
        )

    [13] => Array
        (
            [0] => 2600
        )

)

Link to comment
https://forums.phpfreaks.com/topic/258860-mysqli_fetch_array-issue/
Share on other sites

For each record, mysqli_fetch_array() returns an array, with the contents of each field in an element. You're putting each array returned by mysqli_fetch_array() into a new element of another array, thereby creating a multidimensional array.

I think i follow what you're saying but even if i do simply echo $row i get "Array" as the result. So it's a multidimensional array regardless.

 

Can you suggest the best way to simply get a numeric array of results?

 

Thanks for your help btw!

 

 

 

For each record, mysqli_fetch_array() returns an array, with the contents of each field in an element. You're putting each array returned by mysqli_fetch_array() into a new element of another array, thereby creating a multidimensional array.

Not to worry. Based on what you said i've worked it out:

 

$rows[] = $row[0];

 

Thanks

 

Hi Guys

 

Using mysqli_fetch_array(), which i had anticipated would simply return a numeric array with all the results from the simple query. However, it keeps coming out as a multidimensional array, which isn;t what i want.

 

Is this just how it works or have i done something wrong?

 

Code

 

$mysqli = new mysqli("localhost", "root", "", "ik");
		if ($mysqli->connect_errno) {
			echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
		}



	$qry = "SELECT amount FROM teams";
	$result = $mysqli->query($qry);

	while($row = mysqli_fetch_array($result, MYSQLI_NUM))
		{
		$rows[] = $row;
		//echo $row . "<br/>";
		}

		print_r($rows);

 

 

Result

 

Array
(
    [0] => Array
        (
            [0] => 3
        )

    [1] => Array
        (
            [0] => 25
        )

    [2] => Array
        (
            [0] => 26
        )

    [3] => Array
        (
            [0] => 31
        )

    [4] => Array
        (
            [0] => 34
        )

    [5] => Array
        (
            [0] => 44
        )

    [6] => Array
        (
            [0] => 50
        )

    [7] => Array
        (
            [0] => 56
        )

    [8] => Array
        (
            [0] => 65
        )

    [9] => Array
        (
            [0] => 221
        )

    [10] => Array
        (
            [0] => 222
        )

    [11] => Array
        (
            [0] => 225
        )

    [12] => Array
        (
            [0] => 2210
        )

    [13] => Array
        (
            [0] => 2600
        )

)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.