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 ) ) 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. 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 ? 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>'; 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. 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]; 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 Link to comment https://forums.phpfreaks.com/topic/229733-while-loop-mysql/#findComment-1183520 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.