2

I try to get the content of my mysql database to my website. For this i need Javascript to work with the data. The Problem is that I just want to use php to get the data out of the database. Thes rest, I want to do with an ajax request, but I don't get this. Here my try:

This is the database.php file

<?php
 $pdo=new PDO("mysql:dbname=markers;host=127.0.0.1","root","");
 $stmt = $pdo->query("SELECT * FROM markers");
 $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
 echo json_encode($results);
?>

And this is my try for the ajax request:

$(function(){
 $.ajax({ 
 url: 'api.php', 
 data: "", 
 dataType: 'json', 
 success: function(data) 
 {
 var id = data['id']; 
 var vname = data['desc']; 
 } 
 }); 
});

The Problem, I don't get the content, I just got undefined returns.

And is this a good solution for a big database? On every pagevisit of a user, the mysql - statement gets execute and the whole db table gets encodes in json for the ajax request.

Thanks :)

Micaela
1321 silver badge13 bronze badges
asked Sep 14, 2015 at 8:48
8
  • Select everything from one big table is definitely not a good idea. Now you should debug your PHP script if its returning data or not. If it returns then on the JS part you have json data so you cant access them as array. Commented Sep 14, 2015 at 8:51
  • The PHP script returns data in Json Format like this: {"id":"1","desc":"Frankie Johnnie & Luigo Too","address":"939 W El Camino Real, Mountain View, CA","lat":"37.386337","lng":"-122.085823"}, Commented Sep 14, 2015 at 8:54
  • You will have to read it like an array. Commented Sep 14, 2015 at 8:55
  • Okay, with ajax? But How? Commented Sep 14, 2015 at 8:57
  • First I would do a Console.log on the data variable to see what response you're getting. Also it returns an object not an array IIRC so you should do data.id not data['id'] Commented Sep 14, 2015 at 9:00

2 Answers 2

1

It is just because you are getting a two dimensional array object ,, try something as below

$(function(){
 $.ajax({ 
 url: 'api.php', 
 data: "", 
 dataType: 'json', 
 success: function(data) 
 {
 //var obj=JSON.parse(data);
 var obj=data;
 for (var x in obj)
 {
 alert(obj[x].id + " AND " + obj[x].desc);
 } 
 } 
 }); 
}); 
answered Sep 14, 2015 at 9:07
Sign up to request clarification or add additional context in comments.

Comments

0

The best way to know exact happening is get the object details from server response

.. You will find the errors while doing this

$(function(){
 $.ajax({ 
 url: 'api.php', 
 data: "", 
 dataType: 'json', 
 success: function(data) 
 {
 alert(JSON.parse(data)); 
 } 
 }); 
});
Viral
3,7531 gold badge21 silver badges34 bronze badges
answered Sep 14, 2015 at 8:55

4 Comments

You could get values while parsing json var ob=JSON.parse(data) ; alert(ob.id);
jQuery will automatically parse a ajax response as JSON - also why would you want to alert a database response, use console.log
@Coderrrr , Ofcourse ,you are right , but alert could be an alternative of console.log during the development or testing ..
Oh Now i Got it. Is It a good Idea to call a JS Function out of the sucess part of the ajax request?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.