Android Tutorial Connect Android to MySQL Database Tutorial

Status
Not open for further replies.

dunski

Member
Licensed User
Longtime User
If it prints the PHP script then your server is misconfigured. Instead of executing the PHP it treats it as a text file.
Hi Erel,
Thanks for getting back to me. Do you mean php is configured incorrectly on the server?
would you mind looking at the phpinfo.php read out from my server please and let me know what you think.


Thank you so much in advance
 

dunski

Member
Licensed User
Longtime User
Hi Erel,
Thanks for getting back to me. Do you mean php is configured incorrectly on the server?
would you mind looking at the phpinfo.php read out from my server please and let me know what you think.



Thank you so much in advance
There is a lot more to the read out than this. I just didn't want to clog up the thread with rubbish.
I suppose I just need to know what would be stopping it from executing a script.
 

dunski

Member
Licensed User
Longtime User
Hi Erel,
It is on a shared hosting platform, where I don't have much control of the server. What do I have to ask the host to do?

Thanks in advance
 

dunski

Member
Licensed User
Longtime User
It just spits this back into the log...



startService: class com.repbuddy.httputils2service
** Service (httputils2service) Create **
** Service (httputils2service) Start **
Response from server: <?



$databasehost = "*********";

$databasename = "LogCat connected to: B4A-Bridge: samsung GT-I9205-357426051908784
--------- beginning of /dev/log/main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (service1) Create **
** Service (service1) Start **
Connected to B4A-Bridge (Wifi)
** Activity (main) Pause, UserClosed = true **
libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
Connected to B4A-Bridge (Wifi)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
Installing file.
PackageAdded: package:com.repbuddy
** Activity (main) Create, isFirst = true **
(Main, 75) FullScreen or IncludeTitle properties in layout file do not match the activity attributes settings. (warning #1004)
** Activity (main) Resume **
(Main, 105) FullScreen or IncludeTitle properties in layout file do not match the activity attributes settings. (warning #1004)
startService: class com.repbuddy.httputils2service
** Service (httputils2service) Create **
** Service (httputils2service) Start **
Response from server: <?



$databasehost = "*********";

$databasename = "*********";

$databaseusername ="*********";

$databasepassword = "*********";



$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());

mysql_select_db($databasename) or die(mysql_error());

mysql_query("SET CHARACTER SET utf8");

$query = file_get_contents("php://input");

$sth = mysql_query($query);



if (mysql_errno()) {

header("HTTP/1.1 500 Internal Server Error");

echo $query.'\n';

echo mysql_error();

}

else

{

$rows = array();

while($r = mysql_fetch_assoc($sth)) {

$rows[] = $r;

}

print json_encode($rows);

}

?>
main_jobdone (B4A line: 252)
elist = parser.NextArray 'returns a list with maps
java.lang.RuntimeException: Object should first be initialized (JSONParser).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:46)
at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:57)
at com.repbuddy.main._jobdone(main.java:1085)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.keywords.Common$5.run(Common.java:958)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5296)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object should first be initialized (JSONParser).
";

$databaseusername ="*********";

$databasepassword = "*********";



$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());

mysql_select_db($databasename) or die(mysql_error());

mysql_query("SET CHARACTER SET utf8");

$query = file_get_contents("php://input");

$sth = mysql_query($query);



if (mysql_errno()) {

header("HTTP/1.1 500 Internal Server Error");

echo $query.'\n';

echo mysql_error();

}

else

{

$rows = array();

while($r = mysql_fetch_assoc($sth)) {

$rows[] = $r;

}

print json_encode($rows);

}

?>
main_jobdone (B4A line: 252)
elist = parser.NextArray 'returns a list with maps
java.lang.RuntimeException: Object should first be initialized (JSONParser).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:46)
at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:57)
at com.repbuddy.main._jobdone(main.java:1085)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.keywords.Common$5.run(Common.java:958)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5296)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object should first be initialized (JSONParser).
 

andre.astafieff

Member
Licensed User
Longtime User
Hello, I'm trying an insert, but nothing happens, it does not get the error message.

instruction:ExecuteCommandQuery("INSERT INTO cidades_br.teste2(foto) VALUES ('teste');",EXECUTA_COMANDO)
 

andre.astafieff

Member
Licensed User
Longtime User


yes, I checked, but nothing is inserted ...
I will review all the code again observe if not forgotten something.
 

dunski

Member
Licensed User
Longtime User

Hi Erel,
Thanks again for getting back to me.
The hosting company said the only thing that would have changed since I had the script working before on their server would be the php version which is now at 5.5.6. I tried downgrading it to 5.4.22 but I got the same result.
Any other Ideas?
Thanks for your help on this.
 

dunski

Member
Licensed User
Longtime User
Try to run the script from the browser. Do you see the script itself?

Hi Erel
I got it working.
I just played around with the script. Striped it down to its most basic until it worked.

So this is what I got to work, Although it is missing the errno from mySql it works and to be honest I dont know why this works over the previous one. Would you know?


Thanks for all you help
Dunski
 

dunski

Member
Licensed User
Longtime User
This solution is not really any good for me actually because I need the error handling from the database.
Erel do you you have any Idea why I can't include the error handling in the script.
When I run it in the browser it just returns nothing, a white screen.
Please help
 

dunski

Member
Licensed User
Longtime User
Ok It seems I need to use mySqli as I am using version 5.5 MySql database.

I'm not too hot on PHP but this script needs to be updated for people using MySql 5.5

If anyone knows how to do that It would be helping me and anyone else wanting to use this script on mySql 5.5.

Thanks in advance
 

dunski

Member
Licensed User
Longtime User
It should work with MySQL 5.5. Starting from PHP 5.5. You might see warnings about usage of deprecated messages. You can configure PHP to ignore these warnings.

Thank you so much Erel!
I finally got it working perfect.
I found this line of php on stack overflow that ignores warnings about usage of deprecated messages
PHP:
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);
Did the trick

So This is exactly how it works if you are using mysql 5.5 and php 5.5 also...
PHP:
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);

$con = mysql_connect("Host","UserName","Password") or die(mysql_error());
mysql_select_db("DBName") or die(mysql_error());

mysql_query("SET CHARACTER SET utf8");

$query = file_get_contents("php://input");

$sth = mysql_query($query);

if (mysql_errno()) {
    header("HTTP/1.1 500 Internal Server Error");
    echo $query.'\n';
    echo mysql_error();
}
else
{
    $rows = array();
    while($r = mysql_fetch_assoc($sth)) {
        $rows[] = $r;
    }
    print json_encode($rows);
}
?>
 

asawyer13

Member
Licensed User
Longtime User


Can you tell me how you are zipping and encrypting in the php code and then unzipping and decrypting on the b4a side?
 

jalle007

Active Member
Licensed User
Longtime User

Isn't this related to SQLlite ?

I need to read images stored in BLOB field in MySQL db (not SQLite) located remotely.
Surprisingly could not find any samples , and the one I tried did not get me any results

B4X:
Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
    Dim res As String

        res = Job.GetString
        Log("Response from server: " & res)
        Dim parser As JSONParser
        parser.Initialize(res)
      
        Select Job.jobname
            Case jobname
                Dim articles As List
                articles = parser.NextArray
                For i = 0 To articles.Size - 1
                    Dim m As Map
                    m = articles.Get(i)
                    m.
                    Dim art As Article
                    art.image= m.Get("image")
                    art.id= m.Get("id")
                    art.title= m.Get("title")
                    art.date1= m.Get("date")
                    art.categoryid= m.Get("categoryid")
                    art.imageid= m.Get("imageid")

                    art.content=m.Get("content")
                  
                    'ListView1.AddTwoLines2(tl.First, tl.Second, tl)
                Next
          
        End Select
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

The problem is that for 'image' field I always get result null in res var,
but I am 100% sure that the image is already stored in the database.

So how do I retrieve image from db?
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…