I'm trying to access a MySQL DB with PHP in order to populate a Javascript array, but the array always comes up empty. Before, I had a 'www-data@localhost' access error, but couldn't figure it out and have since rolled back the code. Can anyone share any insight?
JS:
var arrayPOIs = [];
function getPoints(){
$.get('getdata.php', function(data){
arrayPOIs = data;
});
}
getdata.php:
$mysqli = new mysqli("localhost", "user", "pass", "test");
if (mysqli_connect_errno())
{
echo("Connect failed: ") . mysqli_connect_error();
exit();
}
$query = "SELECT * FROM points";
$result = mysqli_query($query) or die(mysqli_error());
$potential = mysqli_fetch_array($result);
echo json_encode($potential);
I was trying to output the contents of the table to an array. In my browser's console it's always showing up empty. Such as:
arrayPOIs
""
I think I might have some issue with the DB connection query process. Although, I'm quite new to this and I'm lost. Any ideas?
Additionally, how and where can I check what PHP is doing realtime, ie, the error messages?
EDIT:
Following up on the comments:
after adding
"json"
as a third parameter, thearrayPOIs
var became (in FF's console)Array [ ]
my
getdata.php
page comes out empty (blank) when accessed directly
EDIT2:
After Niranjan N Raju's help (+1), it's partially fixed. Bad php syntax on my query call. Now, my object is ill formed. I'm getting:
{"0":"1","id":"1","1":"poi_teste1","name":"poi_teste1","2":"41.1953","latitude":"41.1953","3":"-8.60134","longitude":"-8.60134"}
But I need something like (JSON):
name: poi_teste1,
latitude: 3464357247,
longitude: 247245672
for each of the rows in the table, which is something like this:
id | name | latitude | longitude
#1 | string | float | float
#2 | string | float | float
#3 | string | float | float
#4 | string | float | float
json
as a third parameter to your$.get
function? – Jacques Marais Oct 19 at 16:39getdata.php
page from your browser, and tell us what it returns. – Jacques Marais Oct 19 at 16:40mysqli_fetch_assoc
. – FirstOne Oct 19 at 17:02