No response on database call

#1

I try to get a value from a database. It is sensor data, which i wanna save in from an odroid on the database.

The problem is have is, that i cannot get the value into my ionic 3 app.
The Php file should work, when i open the file, i get the right number in the browser. for testing i also took just a .txt-file with the number 9 inside. but it still doesn´t work. It still stays empty. I have global variables into the provider, which i wanna hold up-to-date.

after some tries i get a value back, but it is always not a number

here my new code:

global.ts (provider)

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';



@Injectable()
export class GlobalProvider {

dif = [1,2,3,4,55,6,7,8,9]


constructor(public http: HttpClient){}

load() {
    
    this.http.get('192.168.131.221/neu/fetch_data.php').
    toPromise().then(result => {
    this.dif[0] = parseInt(JSON.stringify(result));
})
}


    
}

php:

<?php
$servername = "url";
$username = "usre";
$password = "pass";
$dbname = "cc";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT wert FROM ultraschall WHERE id=1";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {

        header('Content-Type: application/json');
        echo $row["wert"];
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?> 

data.html (call of the provider)

   <!--here starts the left text box (Ultrasonic)-->
    <button ion-button (click)="global.load()">push</button>
    <div class="llDataContainer">
        <div class="llDataBox llDataBox-2"></div>
        <div class="llDataBox llDataBox-2"></div>
        <div class="llDataBox llDataBox-2">ultra1:</div>

        <!--distance value (ultra1)-->
        <div class="llDataBox llDataBox-2">{{ global.dif[0] }} cm</div>    

if i push the button, still it change the number 1 in naN.

#2

Are you reading the content of file or the executing API call?

#3

i want to read the content. now it´s a text file with just one number inside. but i also tried it before with this php file

<?php


$host = "192.168.131.221";
$user = "root";
$passwort = "pass";
$dbName = "xxx";


$db_connection = mysqli_connect($host,$user,$passwort,$dbName);

if ($db_connection) {

    $utfQuery = "SET NAMES 'utf8'";
    mysqli_query($db_connection, $utfQuery);

    $query = "SELECT wert FROM ultraschall WHERE id = 1";
    
    $result = mysqli_query($db_connection, $query);

    $ult1 = $result->fetch_all();

    echo json_encode($ult1);

    
    
}

else {
    die("Die Verbindung zur Datenbank ist fehlgeschlagen");
}
?>

had the same problem. when i open it in the browser it works, but nothing comes into the app…

#4

Implement the API call to read that number. In your IONIC application use AJAX call to get the value and check the result. if still problem share the API information I will share the code to you

#5

i have no experience with ajax, but i think it´s more a method to hold it up-to-date always. that would work perfect with angular. i just don´t get the content

#6

Can you try data.text()?

#7

@xerintel how do you mean it?

#8

I use it this way with json_encode:

In the .php file, I create an array like

$response = array();
$response[‘number’] = 1;

then, in my http.get:

this.http.get(‘url_link’).
toPromise().then(response => {
console.log(response.json().number)
})

and what I ment with my last response was to change this line:

this.dif1 = parseInt(JSON.stringify(data));

to

this.dif1 = parseInt(JSON.stringify(data.text()));

#9

yes I tried it with data.text(), but not working. also the other I tried on this way:

php:

<?php


$host = "192.168.xxx.xxx";
$user = "root";
$passwort = "pass";
$dbName = "cc";


$db_connection = mysqli_connect($host,$user,$passwort,$dbName);

if ($db_connection) {

    $utfQuery = "SET NAMES 'utf8'";
    mysqli_query($db_connection, $utfQuery);

    $query = "SELECT wert FROM ultraschall WHERE id = 1";
    
    $result = array();
    $result[‘number’] = 1;

    $result = mysqli_query($db_connection, $query);

    $ult1 = $result->fetch_all();

    echo json_encode($ult1);

    
    
}

else {
    die("Die Verbindung zur Datenbank ist fehlgeschlagen");
}
?>

and in global.ts:

load() {
    

    this.http.get('url').
    toPromise().then(result => {
    this.dif1 = parseInt(JSON.stringify(result));
})
}

the good thing is, that i get a response now. the bad thing: my response is always NAN…

#10

check fetch_all() returns something or not by print_r($ult1) or var_dump($ult1)

#11

when i open it in browser, it prints me [[“9”]]
but when i try the php it´s still empty, just when I use the textfile with just the number 9 inside, which i uploaded on the server, then i get NAN back

#12

i changed it now a bit (edited mainpost), but now i always get naN…

console.log(result) gives also Null