jarv Posted July 22, 2016 Share Posted July 22, 2016 (edited) Please help, I am trying to get data out of my database as an array but it doesn't seem to be working?! I just get: ArrayArrayArrayArrayArray here is my code <?php $mysqli = mysqli_connect("localhost", "root", "", "plods"); $sql_locations = "SELECT * FROM southdowns"; $rs_locations = mysqli_query($mysqli, $sql_locations); foreach( $rs_locations as $rs_location ) { $markers[] = array( "{$rs_location['address1']}, {$rs_location['address2']}", $rs_location['Longitude'], $rs_location['Latitude'] ); echo $markers; } ?> Edited July 22, 2016 by jarv Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/ Share on other sites More sharing options...
Psycho Posted July 22, 2016 Share Posted July 22, 2016 (edited) You can't "echo" an array (it will just output the word Array as you have seen. You either need to iterate through the array elements and echo the values or you can use print_r() to output the whole array. Also, do the string concatenation in the query instead of the php logic. Try this <?php $mysqli = mysqli_connect("localhost", "root", "", "plods"); $sql_locations = "SELECT CONCAT(address1, ', ', address2) as address, Longitude, Latitude FROM southdowns"; $rs_locations = mysqli_query($mysqli, $sql_locations); while( $rs_location = $rs_locations->fetch_assoc() ) { $markers[] = $rs_location; } echo "<pre>".print_r($markers, 1)."</pre>"; ?> Edited July 22, 2016 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534843 Share on other sites More sharing options...
jarv Posted July 22, 2016 Author Share Posted July 22, 2016 Thanks, that worked but now I get: Uncaught RangeError: Maximum call stack size exceeded Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534847 Share on other sites More sharing options...
jarv Posted July 22, 2016 Author Share Posted July 22, 2016 (edited) it needs to be out put like this: array( 'Palace of Westminster, London', 51.499633, -0.124755 ), array( 'Westminster Abbey, London', 51.4992453, -0.1272561 ) but I am getting: Array ( [0] => Array ( [address] => Quintessence Fragrances Ltd, Unit 2A, Hawthorne Industrial Estate [Longitude] => 0.058305 [Latitude] => 50.798006 ) [1] => Array ( [address] => Nyetimber, Broughton House, 6-8 Sackville Street [Longitude] => -0.137792 [Latitude] => 51.509285 ) Edited July 22, 2016 by jarv Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534848 Share on other sites More sharing options...
Jacques1 Posted July 22, 2016 Share Posted July 22, 2016 it needs to be out put like this: Why? What are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534852 Share on other sites More sharing options...
jarv Posted July 22, 2016 Author Share Posted July 22, 2016 convert it to JSON and plot markers on a map, like here: http://stackoverflow.com/questions/33652498/google-map-to-display-many-markers-dynamically-using-php-and-jquery Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534854 Share on other sites More sharing options...
Jacques1 Posted July 22, 2016 Share Posted July 22, 2016 The example output you claim to need isn't JSON. If you want JSON, use json_encode() together with the right Content-Type header: <?php header('Content-Type: application/json'); $data = ['a' => 1, 'b' => 2]; echo json_encode($data); Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534857 Share on other sites More sharing options...
jarv Posted July 22, 2016 Author Share Posted July 22, 2016 no no, if you look at the working example on that link when you call json_encode() in the javascript it should then look like this: array( 'Westminster Abbey, London', 51.4992453, -0.1272561 ), array( 'QEII Centre, London', 51.4997296, -0.128683 ) Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534860 Share on other sites More sharing options...
Psycho Posted July 22, 2016 Share Posted July 22, 2016 (edited) no no, if you look at the working example on that link Um, no. You are incorrect. The "working example" on that page is PHP code that defines an hard-coded array for testing purposes instead of getting the values from a DB. The author even includes the DB logic commented out showing that is how it would really be done. Plus, the way an array is defined looks different than when you output an array using print_r(). The print_r() function is really more of a debugging tool to output an array showing the structure and the values. Expecting that to look exactly like the definition of the array is foolish. The only difference between the hard-coded array in that example and the array built from the code I provided is that the array in that example code has numerically based indexes (i.e. 0, 1, 2) rather than named indexes as will be present from a DB query. I don't know if the values of the indexes will matter or not. But, if they do need to be numerical indexes, then convert the array to remove the named indexes and replace with numerical ones. I would show you how to do that, but apparently you know what you are doing, and don't need any assistance from us. EDIT: And just to be 100% clear. The "JSON Array" code that you are pointing to is not JSON. It is simply the definition of a PHP array. It would still need to be converted to JSON (if that's really what you need). Edited July 22, 2016 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534862 Share on other sites More sharing options...
Jacques1 Posted July 22, 2016 Share Posted July 22, 2016 no no, if you look at the working example on that link You're wrong. And it's not really a good idea to reject valid answers when you haven't even tried the code and don't understand the topic. Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534863 Share on other sites More sharing options...
jarv Posted July 25, 2016 Author Share Posted July 25, 2016 (edited) ok, I don't get it, sorry. I am just trying to follow the example using the php array they commented out here is where I currently am: $mysqli = mysqli_connect("localhost", "root", "", "plods"); $sql_locations = "SELECT CONCAT(address1, ', ', address2) as address, Longitude, Latitude FROM southdowns"; $rs_locations = mysqli_query($mysqli, $sql_locations); while( $rs_location = $rs_locations->fetch_assoc() ) { foreach( $rs_locations->fetch_assoc() as $rs_location => $value){ $markers[] = $value; } } //while( $rs_location = $rs_locations->fetch_assoc() ) { // $markers[] = $rs_location; //} echo "<pre>".print_r($markers)."</pre>"; and it comes out like this: Array ( [0] => Nyetimber, Broughton House, 6-8 Sackville Street [1] => -0.137792 [2] => 51.509285 [3] => Baker Tilly, The Portland Building, 25 High Street..... Edited July 25, 2016 by jarv Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534985 Share on other sites More sharing options...
Barand Posted July 25, 2016 Share Posted July 25, 2016 You didn't say if key names were required or not, so you want either while( $rs_location = $rs_locations->fetch_assoc() ) { $markers[] = $rs_location; } or while( $rs_location = $rs_locations->fetch_row() ) { $markers[] = $rs_location; } followed by $json = json_encode($markers); 1 Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534989 Share on other sites More sharing options...
jarv Posted July 25, 2016 Author Share Posted July 25, 2016 Thanks, I'm getting there now, I used the second one: while( $rs_location = $rs_locations->fetch_row() ) { $markers[] = $rs_location; } My markers are now showing, GREAT! .... but in the middle of the Indian Ocean Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534997 Share on other sites More sharing options...
jarv Posted July 25, 2016 Author Share Posted July 25, 2016 Lat/Long was wrong way round! THANKS! Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1534998 Share on other sites More sharing options...
Barand Posted July 25, 2016 Share Posted July 25, 2016 Unfortunately for you, not all appear to be reversed. I have spotted a dozen in the above data that seem to be the correct way round (although in the North Atlantic). For example, the first three that are visible in the above image. Quote Link to comment https://forums.phpfreaks.com/topic/301545-null-is-output-in-array/#findComment-1535000 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.