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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
        )

)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.