Drongo_III Posted March 13, 2012 Share Posted March 13, 2012 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/258860-mysqli_fetch_array-issue/ Share on other sites More sharing options...
Pikachu2000 Posted March 13, 2012 Share Posted March 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258860-mysqli_fetch_array-issue/#findComment-1327037 Share on other sites More sharing options...
Drongo_III Posted March 13, 2012 Author Share Posted March 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258860-mysqli_fetch_array-issue/#findComment-1327039 Share on other sites More sharing options...
Pikachu2000 Posted March 13, 2012 Share Posted March 13, 2012 You can't use echo to output the contents of an array. Quote Link to comment https://forums.phpfreaks.com/topic/258860-mysqli_fetch_array-issue/#findComment-1327040 Share on other sites More sharing options...
Drongo_III Posted March 13, 2012 Author Share Posted March 13, 2012 Ok that makes sense. So to get a numeric array from this query what would i have to do? I am a tad lost... You can't use echo to output the contents of an array. Quote Link to comment https://forums.phpfreaks.com/topic/258860-mysqli_fetch_array-issue/#findComment-1327041 Share on other sites More sharing options...
Drongo_III Posted March 13, 2012 Author Share Posted March 13, 2012 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/258860-mysqli_fetch_array-issue/#findComment-1327045 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.