MetalSmith Posted March 6, 2011 Share Posted March 6, 2011 Anyone know why I get this from my mysql query?It adds an extra key for some reason. $types = array(); while ($row = mysql_fetch_array($query_name,MYSQL_NUM)) { $types[] = $row; } print_r ($types); Array ( [0] => Array ( [0] => Matt ) [1] => Array ( [0] => Larissa ) [2] => Array ( [0] => Braden ) [3] => Array ( [0] => Tom ) ) Quote Link to comment https://forums.phpfreaks.com/topic/229733-while-loop-mysql/ Share on other sites More sharing options...
jcbones Posted March 6, 2011 Share Posted March 6, 2011 Yes, each $row is already an array, then you are making another array holding all of the rows. Quote Link to comment https://forums.phpfreaks.com/topic/229733-while-loop-mysql/#findComment-1183432 Share on other sites More sharing options...
MetalSmith Posted March 6, 2011 Author Share Posted March 6, 2011 How can I stop that ? Quote Link to comment https://forums.phpfreaks.com/topic/229733-while-loop-mysql/#findComment-1183433 Share on other sites More sharing options...
jcbones Posted March 6, 2011 Share Posted March 6, 2011 I don't understand your question? It is NOT adding an extra key, all keys have a value. Try wrapping the print_r() function in <pre> tags. echo '<pre>'; print_r($types); echo '</pre>'; You will find you have a multi-dim. array, just like you ask PHP to build for you. If you don't want it multi-dim. then, $types = array(); while ($row = mysql_fetch_array($query_name,MYSQL_NUM)) { $types[] = $row[0]; } echo '<pre>'; print_r($types); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/229733-while-loop-mysql/#findComment-1183436 Share on other sites More sharing options...
MetalSmith Posted March 6, 2011 Author Share Posted March 6, 2011 Well I want just the name, but now the value is a key + name. Now if I need the name then I have to strip off the Array ( [0] => if front of the name is all. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/229733-while-loop-mysql/#findComment-1183437 Share on other sites More sharing options...
jcbones Posted March 6, 2011 Share Posted March 6, 2011 Well you don't have to strip anything, just echo it. echo $types[0] . ' , ' . $types[3]; Quote Link to comment https://forums.phpfreaks.com/topic/229733-while-loop-mysql/#findComment-1183442 Share on other sites More sharing options...
sasa Posted March 6, 2011 Share Posted March 6, 2011 try $types = array(); while ($row = mysql_fetch_array($query_name,MYSQL_NUM)) { $types[] = $row[0]; } print_r ($types);[/codeg Quote Link to comment https://forums.phpfreaks.com/topic/229733-while-loop-mysql/#findComment-1183520 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.