How to get my data back


christian goose

I am new to dynamic data and trying to

  1. Read data from mysql database using PHP
  2. Convert this data to JSON
  3. Extract data via javascript (jQuery)
  4. Insert it into my page.

As far as I can tell, 1) and 2) work, 3) seems to work, and 4) solves the problem.

I have set up the database and tables, querying and echoing the data like this (api.php)

<?php 

  include 'db.php';
  $con = new mysqli($host,$user,$pass,$databaseName);

  /* check connection */
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

  $query = "SELECT * FROM $tableName";
  $myArray = array();

  if ($result = $con->query($query)) {
    $tempArray = array();
    while($row = $result->fetch_object()) {
      $tempArray = $row;
      array_push($myArray, $tempArray);
    }
    echo json_encode($myArray);
  }

  $result->close();
  $con->close();

?>

Now I get it via javascript...

jQuery(function($) {    
    $.ajax({                                      
        url: 'php/api.php',
        data: "json",
        dataType: "",    
        success: function(data) {
            var r = new Array(), j = -1;
            for (var key=0, size=data.length; key<size; key++){
                r[++j] ="<tr><td>";
                r[++j] = data[key][0];
                r[++j] = "</td><td>";
                r[++j] = data[key][1];
                r[++j] = "</td><td>";
                r[++j] = data[key][2];
                r[++j] = "</td></tr>";
            }
            var joined = r.join('');
            console.log(joined);
            $('#maintable tbody').html(joined);    
        }
    });
});

...and throw it into the tbody element of my table.

The result is definitely not what I expected:

See http://i.imgur.com/hRNrmdC.jpg (sorry for some german interface)

The answer to the GET request is at least theoretically valid JSON ( checked with http://jsonlint.com/ ), but to me the data seems to be treated as a string and split into an array of chars (each char a "key" in data[], so "data[key][1] returns nothing.

var json = JSON.stringify(eval("(" + data + ")"));

and keep using json[key][0] doesn't help...

Now, I want to know why, especially where is the problem and how to fix it .

Jeroen

You have mixed the keys in the ajax call:

    data: "json",
    dataType: "",    

should:

    dataType: "json",

Because you don't send or use any data at all.

Related


How to get my data back

christian goose I am new to dynamic data and trying to Read data from mysql database using PHP Convert this data to JSON Extract data via javascript (jQuery) Insert it into my page. As far as I can tell, 1) and 2) work, 3) seems to work, and 4) solves the prob

How do I get my data back from my IpcMain

Nelz I am creating a drag and drop react and using electron to send and receive my file data. I received senderId and sender in console.log. How can I get the data to appear? What did I miss? Dropzone.js file const [fileNames, setFileNames] = useState([]); co

How to get my data back from a button to another php page

Dawai Tomek I have a button on a php page that has some data on it. My button redirects to another PHP page and I want to get this data back. i try to do if(isset($_POST['button'])){ echo($data); } isset is correct (I tested with strings) but cannot get d

How to get my data back from a button to another php page

Dawai Tomek I have a button on a php page that has some data on it. My button redirects to another PHP page and I want to get this data back. i try to do if(isset($_POST['button'])){ echo($data); } isset is correct (I tested with strings) but cannot get d

How to Get Back Windows Data

username I reinstalled Ubuntu a few months ago. By accident, I chose to install Ubuntu without Windows (I had a dual-boot OS before). This is a disaster! I lost all my data in Windows and also the OS. Do you know if I can restore the OS? Bring Windows back to

How to Get Back Windows Data

username I reinstalled Ubuntu a few months ago. By accident, I chose to install Ubuntu without Windows (I had a dual-boot OS before). This is a disaster! I lost all my data in Windows and also the OS. Do you know if I can restore the OS? Bring Windows back to

How to Get Back Windows Data

username I reinstalled Ubuntu a few months ago. By accident, I chose to install Ubuntu without Windows (I had a dual-boot OS before). This is a disaster! I lost all my data in Windows and also the OS. Do you know if I can restore the OS? Bring Windows back to

How do I get my stuff back?

clown ok, about 10 minutes ago, i put this .py file on my desktop, changed it to executable, when i double clicked it, it opened, all the files on my desktop disappeared, it Created a folder called "Key Database". Does anyone know how to get my stuff back? Tha

How do I get my WordPress back?

display key I have installed bitnami in c:\BitNami (wamp stack is 5.4.24) on Windows XP on local disk and in "c:\BitNami\wampstack-5.4.24-0\apache2\htdocs\wordpress" With wordpress installed, I do some blogging on wordpress. A few days ago my XP crashed and I

How do I get my stuff back?

clown ok, about 10 minutes ago, i put this .py file on my desktop, changed it to executable, when i double clicked it, it opened, all the files on my desktop disappeared, it Created a folder called "Key Database". Does anyone know how to get my stuff back? Tha

How do I get my WordPress back?

display key I have installed bitnami in c:\BitNami (wamp stack is 5.4.24) on Windows XP on local disk and in "c:\BitNami\wampstack-5.4.24-0\apache2\htdocs\wordpress" With wordpress installed, I do some blogging on wordpress. A few days ago my XP crashed and I

How to get data back from angular instruction

username I hope someone can help me with an example because the edges and corners are driving me crazy :( The formula I made the first time should follow this structure: Angular APP mainController ----> smallController1 --------> otherElements ----> smallContr

How to get data back with $.post call?

Janisoplin I don't want to refresh the page when searching through the database. on the post so I got help with using a phone that $.postcan send information . There is .done(function( data ){one line I haven't used yet. I had this problem too, I'm not sure if

How to get data back from angular instruction

username I hope someone can help me with an example because the edges and corners are driving me crazy :( The formula I made the first time should follow this structure: Angular APP mainController ----> smallController1 --------> otherElements ----> smallContr

How to get data back from a function in firebase

User 9722951 I can see that the function is working now in the cloud function log, it sends back the user info based on the user's email, however, I can't seem to retrieve that data in the app client, it just goes throughOptional(<FIRHTTPSCallableResult: 0x282

How to get data back from angular instruction

username I hope someone can help me with an example because the edges and corners are driving me crazy :( The formula I made the first time should follow this structure: Angular APP mainController ----> smallController1 --------> otherElements ----> smallContr

How to get data back with $.post call?

Janisoplin I don't want to refresh the page when searching through the database. on the post so I got help with using a phone that $.postcan send information . There is .done(function( data ){one line I haven't used yet. I had this problem too, I'm not sure if

How to get data back from the command bus?

say I'm pretty new to domain driven design concepts and I'm having a problem returning the correct response in the API when using a command bus with domain logic and a command bus with command handlers. Suppose we are building an application using a domain-dri

How to get data back from the command bus?

say I'm pretty new to domain driven design concepts and I'm having a problem returning the correct response in the API when using a command bus with domain logic and a command bus with command handlers. Suppose we are building an application using a domain-dri

How to get data back from a function in firebase

User 9722951 I can see that the function is working now in the cloud function log, it sends back user info based on the user's email, however, I can't seem to retrieve that data in the app client, it just goes throughOptional(<FIRHTTPSCallableResult: 0x2825b0b

How to get back user data on Parse?

Dan Boujenah I want to get the data in "requestActive" in "Users" on Parse.t I try like this: var user = PFUser() var requestActive = user["requestActive"] But: error "Instance member 'user' cannot be used on type 'viewController' Eric Aya You are declaring u

How to get data back from angular instruction

username I hope someone can help me with an example because the edges and corners are driving me crazy :( The formula I made the first time should follow this structure: Angular APP mainController ----> smallController1 --------> otherElements ----> smallContr

How to get back data from emberjs controller

Yegor Gorodov I have a list of routes App.Router.map ()-> @resource 'about' @resource 'users', -> @resource 'repositories', path: '/:user_name/repositories' @route 'show', path: '/:user_name' In my user display template I also have a view {{#view App

How to get data back after Ubuntu installation?

284 My friend's laptop is running Windows. It crashed (virus) so I suggested him to install Ubuntu. What I did (Ubuntu 12.04 LTS). Before installation, there are 3 partitions: C:\Location where the system is installed (Windows XP) D:\Where to save some data E:

How to get data back from angular instruction

username I hope someone can help me with an example because the edges and corners are driving me crazy :( The formula I made the first time should follow this structure: Angular APP mainController ----> smallController1 --------> otherElements ----> smallContr

How to get data back with $.post call?

Janisoplin I don't want to refresh the page when searching through the database. on the post so I got help with using a phone that $.postcan send information . There is .done(function( data ){one line I haven't used yet. I had this problem too, I'm not sure if

How to get back user data on Parse?

Dan Boujenah I want to get the data in "requestActive" in "Users" on Parse.t I try like this: var user = PFUser() var requestActive = user["requestActive"] But: error "Instance member 'user' cannot be used on type 'viewController' Eric Aya You are declaring u

How to get back data from emberjs controller

Yegor Gorodov I have a list of routes App.Router.map ()-> @resource 'about' @resource 'users', -> @resource 'repositories', path: '/:user_name/repositories' @route 'show', path: '/:user_name' In my user display template I also have a view {{#view App

How to get data back after Ubuntu installation?

284 My friend's laptop is running Windows. It crashed (virus) so I suggested him to install Ubuntu. What I did (Ubuntu 12.04 LTS). Before installation, there are 3 partitions: C:\Location where the system is installed (Windows XP) D:\Where to save some data E: