<?php
// edit these strings so the script can connect
$database_name='';
$host='';
$password='';
$tablename='';
$username='';
// the array $response will eventually be converted to a JSON object and output as text by the script
$response=array();
$database=mysql_connect($host, $username, $password);
if($database==false){
$response['debug']='Failed to connect to the database: ' . mysql_error();
} else {
if(mysql_select_db($database_name, $database)==false){
$response['debug']='Failed to select the database: ' . mysql_error();
} else {
mysql_query('set names utf8');
$query="SELECT * FROM $tablename";
$result=mysql_query($query);
if($result==false){
$response['debug']='A database error has occurred'.mysql_error();
} else {
while ($row = mysql_fetch_assoc($result)) {
$response[]=$row;
}
}
}
}
ob_start('ob_gzhandler'); // comment/uncomment to enable gzip compression on the JSON output by this script
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json; charset=UTF-8');
echo utf8_encode(json_encode($response));
?>
but I always get this message if I try to access my php script from browser:
"debug":"A database error has occurredQuery was empty"}
obviously the table exists and is not empty (I access It from php myadmin)
$result=mysql_query($result);
$result=mysql_query($query);
And what about any PHP-template of receiving\updating MySQL from B4A ?
Maybe getting a file from B4A (uploading) and updating the MySQL table ?
Sub CreateJson
Dim PostData As Map
PostData.Initialize
Dim InsertData As List
InsertData.Initialize
Dim NewRow As Map
NewRow.Initialize
NewRow.Put("name", "Martin")
NewRow.Put("postcode", "PE30 2DU")
NewRow.Put("age", 44)
InsertData.Add(NewRow)
Dim NewRow As Map
NewRow.Initialize
NewRow.Put("name", "Zedolf")
NewRow.Put("postcode", "Unknown")
NewRow.Put("age", 12)
InsertData.Add(NewRow)
PostData.Put("insert", InsertData)
Dim Generator As JSONGenerator
Generator.Initialize(PostData)
Log("The data to be POST'd to the PHP script is:")
Log(Generator.ToString)
End Sub
{"insert":[{"postcode":"PE30 2DU","age":44,"name":"Martin"},{"postcode":"Unknown","age":12,"name":"Zedolf"}]}
<?php
// edit these strings so the script can connect to your database
$database_name = '';
$host = '';
$password = '';
$tablename = '';
$username = '';
$response = array('status'=>'SUCCESS');
if (isset($_POST['json'])) {
$database = mysql_connect($host, $username, $password);
if ($database == false) {
$response['debug'] = 'Failed to connect to the database: ' . mysql_error();
$response['status']='FAIL';
} else {
if (mysql_select_db($database_name, $database) == false) {
$response['debug'] = 'Failed to select the database: ' . mysql_error();
$response['status']='FAIL';
} else {
mysql_query('set names utf8');
// first get the POST'd json string, escaping it to prevent injection attacks
$json=mysql_real_escape_string($_POST['json']);
// convert the string into an object
$json=json_decode($json, true);
// the json object can have 'insert' and/or 'update' properties
// each property will be an indexed array of associative arrays
if(isset($json['insert'])){
$insert=$json['insert'];
foreach($insert as $row){
$column_names=array();
$column_values=array();
foreach($row as $key=>$value){
$column_names[]=$key;
$column_values="'$value'";
}
$column_names=implode(',', $column_names);
$column_values=implode(',', $column_values);
$query="INSERT INTO $tablename ($column_names) VALUES ($column_values)";
$result=mysql_query($query);
if($result==false){
if(!isset($response['failed_inserts'])){
$response['failed_inserts']=array();
$response['status']='ERRORS';
}
$response['failed_inserts'][]=$query.'|'.mysql_error();
}
}
}
// to do: process the json 'update' array
}
}
} else {
$response['debug']='no json data found in request';
$response['status']='FAIL';
}
ob_start('ob_gzhandler'); // comment/uncomment to enable gzip compression on the JSON output by this script
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json; charset=UTF-8');
echo utf8_encode(json_encode($response));
?>
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?