Connect to a mysql database via php


#1

Hello I try ti get data from a mysql server, so I a write a file on my server “API/waypoints.php” who read all the lines of my database and convert it to Json. When i go at this address with my browser it works. Well, ember’s side I go to “adapter/application.js” and put this

import DS from ‘ember-data’;

export default DS.JSONAPIAdapter.extend({ namespace: ‘API’, host: ‘https://192.168.0.24’ });

when I make : this.store.findAll(‘waypoint’), It didn’t work because the the url made by the adapter is https://192.168.0.24/API/waypoints not https://192.168.0.24/API/waypoints.php How can I fix that’s? Server’s side or Ember’s side? thank for help Vincent


#2

I’m new to this too, but it sounds like perhaps you have to extend the default JSONAPIAdapter and override the urlForFindAll method.

https://www.emberjs.com/api/ember-data/3.0/classes/DS.JSONAPIAdapter/methods/urlForFindAll?anchor=urlForFindAll

This should allow you to build the URL properly.

There might be a better way, but this should work.


#3

Thank You It work, very easy to setup


#4

Finaly I success to read data from mysql server I give my code if it can help somebody because I find few information about this subject

**//  adapters/applications.js**
import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({

    urlForFindAll(modelName, snapshot) {
         return 'http://192.168.0.24/waypoints.php';
         }
    });

**// waypoints.php**

<?php
$servername = "localhost";
$username = "lamp";
$password = "lamp";
$dbname = "newdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM tripclic";
$result = $conn->query($sql);

$array = array();

while($row = $result->fetch_assoc())
        {
        $attributes["attributes"] = $row;
        $attributes["id"]         = $row[id];
        $attributes["type"]       = "waypoints";

        array_push($array, $attributes);
        }

// Finally, encode the array to JSON and output the results

$data = [ "data" => $array ];

echo json_encode( $data);

$conn->close();
?>