Android Question MySQL and PHP problem

Status
Not open for further replies.

elproing

Member
Hello everyone,

I have an application on my Android with select and insert statement on MySQL database over .php script on my server. When I do a SELECT statement everything is working perfect. When I do an INSERT statement I get an error in B4A logs wich occurs occasionally.

ResponseError. Reason: Internal Server Error, Response: PHP Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\inetpub\wwwroot\GetDataSQL_B4A.php on line 21
PHP Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in C:\inetpub\wwwroot\GetDataSQL_B4A.php on line 26

My php code in GetDataSQL_B4A.php is:

B4X:
<?php

$databasehost = "localhost";
$databasename = "mydatabase";
$databaseusername ="myuser";
$databasepassword = "mypass";

$con = mysqli_connect($databasehost,$databaseusername,$databasepassword, $databasename) or die(mysqli_error($con));
mysqli_set_charset ($con , "utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con, $query);

if (mysqli_errno($con)) {
    header("HTTP/1.1 500 Internal Server Error");
    echo $query.'\n';
    echo mysqli_error($con);
}
else
{
    $rows = array();
    while($r = mysqli_fetch_assoc($sth)) {
        $rows[] = $r;
    }
    $res = json_encode($rows);
    echo $res;
    mysqli_free_result($sth);
}

mysqli_close($con);
?>

Why Am I getting this error?
 

sorex

Expert
Licensed User
Longtime User
you need to skip the fetching as an insert doesn't return records.

use switch and parameters via post to select what you want to do.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I think you should pass an SQL statement to $query
based on the file_get_contents("php://input") he is posting the query from B4A to the php-script. #2 from @sorex is more approbiate.

Away from that using this Methods opens all doors for any kind of SQL-Injection.
I would NOT, NEVER!, send the complete Query. Only parameters and i would always create the Query in my PHP-Script.
 
Last edited:
Upvote 0

elproing

Member
I have used this code for inserting data into MySQL and it works perfect for me.

B4X:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = file_get_contents("php://input");

    // use exec() because no results are returned
    $conn->exec($sql);
    echo "INSERT OK";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

Thank you everyone for help

Regards
 
Upvote 0

elproing

Member
Hello,

I have another problem. When I insert number with decimal (3.5) in my EditBox and I insert this number in MySQL, I get 3.49990054798 in my table. Why is that?


This is my part of the code in B4A:

B4X:
InsertPressureStartFRM1=FunctionBlocks.FloatWrite(EditPressureStartFRM1)

and my function wich I call

B4X:
Public Sub FloatWrite (EditFloatData As EditText) As Float
   
    Dim FloatData As Float
   
    If    EditFloatData.Text <> "" Then
       
        FloatData= Round2(EditFloatData.Text,1)
       
    Else
        If EditFloatData.Text = "" Then
            FloatData=0.0
        End If
    End If
   
    Return FloatData
   
End Sub
 
Last edited:
Upvote 0
Status
Not open for further replies.
Top