converting json to xml or not - php

I am using PHP files to provide my iOS app with JSON data. Now I am developing an Android app that should use the same PHP files to parse MySQL objects. The problem I have now is that I am creating the Android app following an internet tutorial and it needs the MySQL objects in XML format.
I kindly request your advice to continue working with my Android app.
Should I change my app to be able to use the same PHP files as the iOS app?
Or is there an easy way that after making a copy from the PHP files and then updating them to change the output type from JSON to XML would allow me to use them to be used by the Android app?
As example, here is my PHP to provide JSON objects to my iOS app:
<?php
$host = "localhost"; // host of MySQL server
$user = "hidden"; // MySQL user
$pwd = "hidden"; // MySQL user's password
$db = "hidden"; // database name
// Create connection
$con = mysqli_connect($host, $user, $pwd, $db);
mysql_query("SET NAMES 'utf8'");
// Check connection
if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
// query the application data
$sql = "SELECT * FROM tbempresas";
$result = mysqli_query($con, $sql);
// an array to save the application data
$rows = array();
// iterate to query result and add every rows into array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
// close the database connection
mysqli_close($con);
// echo the application data in json format
echo json_encode($rows);
?>

You definitely should not change the data format from JSON to XML. Using XML as data format in mobile badly affects performance when it comes to speed with which data are sent.
Instead of changing data format use external lib for JSON parsing such as gson.

You can use json_decode() and PEAR::XML_Serializer for converting the output type to XML and then you can use it in the Android app.

Related

How to connect Firebase database to Php scripts?

I am making an android app with Firebase database, and I am stuck at point when I want to connect that Firebase database with my own hosted Php scripts in which I want to perform some action on specific date , on that date it should delete some data from Firebase. FIrebase data on schedule(On specific date) so I am stuck at how to perform this how can be make this connection to firebase database with Php.
Using connection of php, I want to made connection with firebase DB and want to retrieve and also delete some data from Firebase DB.
<?php
define('HOST','localhost');
define('USERNAME', 'root');
define('PASSWORD','');
define('DB','insert2');
$con = mysqli_connect(HOST,USERNAME,PASSWORD,DB);
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "insert into users (username, password) values ('$username','$password')";
if(mysqli_query($con, $sql)){
echo 'success';
}
?>
Please guide me. thanks
You can't connect to Firebase Database with the same way as MySQL.
You need to use one of these lib :
kreait/firebase-php
ktamas77/firebase-php
Or use the REST API.
Or create a Firebase Cloud Function which delete your wanted data and call it from the php with an HTTP trigger.
You can use this php code
$db = 'IP:C:/folders/files.FDB'; // Computer IP:Database file in drive and folders
$username = 'user';
$password = 'pass';
// Connect to database
$dbh = ibase_connect($db,$username,$password);
$sql = 'SELECT * FROM TABLE';
// Execute query
$rc = ibase_query($dbh, $sql);
// Get the result row by row as object
while ($row = ibase_fetch_object($rc)) {
echo $row->table_column;
}
// Release the handle associated with the result of the query
ibase_free_result($rc);
// Release the handle associated with the connection
ibase_close($dbh);
*And don't forget to install firebase extension to Web Server because it is not enabled by default.

Remote Database Access

I need to access a remote database from my iPhone. I have an account on iPage, but as far as i know it does not support remoteMySql. I wrote a php script that will connect to the remote database. Currently i run it on localhost and this makes it of no use on the phone. Can someone tell me what is the best solution to my problem. Is there a way to use remote access on iPage, the database is on another server not iPage. Or can i write an api or something that will run on iPage ? The best thing would be if i can find a free webpage that can host my php script. Had one but its not working anymore. Any suggestions ?
<?php
$username = "qqqq";
$password = "qqqq";
$hostname = "qqq.qqq.qqq.qqq";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("ttbus",$dbhandle)
or die("Could not select examples");
$result = mysql_query("SELECT stations.name, stations.lon, stations.lat, station_display_rows.minutesToArrival,routes.number FROM stations,station_display_rows,routes WHERE stations.id = station_display_rows.stationId AND routes.routeId = station_display_rows.routeId");
while ($row = mysql_fetch_array($result)) {
echo "<h1>";
echo $row{'name'}."<h2>";
echo $row{'lon'}."<h3>";
echo $row{'lat'}."<h4>";
echo $row{'minutesToArrival'}."<p>";
echo $row{'number'};
}
mysql_close($dbhandle);
?>
I am not familiar with iPage, but if you want to access data from a web site, then you should create a REST endpoint that will return your data in JSON format. Your iOS app will then make this connection, grab the data and you can use it in your app. You can store this data in a a local database using Core Data, save it as a file or just use it in memory.
If you need help creating the REST endpoints in PHP, that should be directed to the PHP section or search for some tutorials, there are plenty.
To read the data from your iOS app, you should read this tutorial: NSUrlSession Tutorial DON'T try and use AFNetworking, its overkill for what you need to do.
If you are in shared hosting platform, remote MySQL is not supported in it. It is supported in their VPS/Dedi platform.

How to connect php to a database in vb application?

I have a VB application using a Microsoft SQLServer Database File on my computer. And I want to access the information from that database online using PHP. I already installed Apache and PHP on my computer.
The problem is I don't know how to connect my PHP to the database on my VB application.
First of all, take a look at this link concerning PHP's MS SQL (Microsoft SQL) library.
You need to use the MS SQL functions to connect to the database and retrieve the relevant information you require.
I'm not an expert with MS SQL but you'll need code similar to the following:
$server = "EDDYSPC\SQLEXPRESS"; // Server address
$user = "root";
$pass = "";
$db = "MyDatabase";
$msSQL = mssql_connect($serverm $user, $root);
if (!$msSQL) { // If there was an error connecting to the database
die("An error occurred.");
}
mssql_select_db($db); // Select the datbase to use
$query = mssql_query("SELECT name FROM users WHERE name = 'Eddy'");
while ($row = mssql_fetch_assoc($query)) { // Loop through each returned row
print($row['name'] . "<br />\n");
}
Hopefully this helps.

How to return data fetched from MySQL into a php file as JSON?

I have to return data fetched from MySQL table into a php file as JSON. Here is my code to connect to mysql & get data from it. How could now I return it as JSON.
<?php
$username = "user";
$password = "********";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";
$selected = mysql_select_db("spec",$dbh)
or die("Could not select first_test");
//$rows = array();
$query = "SELECT * FROM user_spec";
$result=mysql_query($query);
//mysql_close($dbh);
?>
Under is full stack that I have to implement. For step 3, I am rendering the list dynamically using user inputs though its not using any engine but directly using input values so I have to see it how to do once I get JSON data. I put the stack so that you people can kindly see it what I have to do when possibly helping me.
the user load an HTML page
the page make an ajax call and get the options as a JSON(either it exists already in the database, or a new option set is generated)
the json is rendered using a JS templating engine (PURE in our case)
the user change something
the same JSON is modified and sent by a POST to the server
the server read that JSON and store it in the database(you would write the data to your file). And then go back to the step 4, to wait for another user change.
Given that only one user_spec row is returned you can use the built in json_encode function:
<?php
$username = "user";
$password = "********";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
//print "Connected to MySQL<br>";
$selected = mysql_select_db("spec",$dbh)
or die("Could not select first_test");
$query = "SELECT * FROM user_spec";
$result=mysql_query($query);
echo json_encode(mysql_fetch_assoc($result));
?>
Should do the trick.
Even if you are using an older version of PHP, you can find a suitable function in the user comments at the json_encode PHP Manual page to use in it's place.
I think what your looking for is json_encode
Unless you're like me using a painfully old version of Symfony (for your sake, I hope not!) your answer is json_encode Zend and CodeIgniter have similar other easy methods.
If you're not lucky enough to have access to that, you can build it manually via foreach loop.
You can check your JSON strings using JSONlint

ms-access database to mysql database by php

How we can convert a ms-access database to mysql database by php…
or
How we can access a ms-access database by php..
For accessing a MS Access database on Windows through ODBC, see here.
How we can convert a ms-access
database to mysql database by php
You can export via an ODBC connector or (if you don't have too many tables) you can export your data to a text file and then import it into MySQL (after creating the tables manually) via LOAD DATA. Right-click on the tables and choose Export for available options.
For more detailed info on migrating from MS Access to MySQL, check out this great article from the MySQL Dev Team:
http://dev.mysql.com/doc/mysql/en/LOAD_DATA.html
How we can access a ms-access database
by php
You can do this easily right through PDO.
You can use this simple code to connect to Access database. I tried this code in PHP,working on Windows XP with XAMPP's Apache server, and using Access 2007 file as a database. Just create your access file and try this :
First go to Start menu > Control Panel > Administrative Tools > Data Sources(ODBC) > System DSN > Add.. > Microsoft Access Driver(.mdb,.accdb) and show your access file. Give name to the connection.
Then write in your *.php file this code:
`
<?php
$host= "host_name";
$user= "user_name";
$pass= "password";
$db_connect=odbc_connect($host,$user,$pass); //connect to access file as database
if (!$db) //In case if you didn't connect , you'll get this error message
{
echo "Can't connect";
exit;
}
$query = "SELECT * FROM table_name"; //pulling data form Access file
$row = odbc_exec($db, $query);
while(odbc_fetch_row($row)
{
$row1 = odbc_result($row,1);
$row2 = odbc_result($row,2);
$row3 = odbc_result($row,3);
echo $row1." ".$row2." ".$row3."<br>"; //watching if data is taken correctly
}
?>
And then you can insert that rows into sql database by adding this code into the while loop :
<?php
$db="MySQLdatabaseName";
$db_connect= mysql_connect($host,$user,$pass);
mysql_select_db($db, $db_connect);
$insert_into_MySQL = "INSERT INTO table_name($row1,$row2,$row3)
VALUES('".$row1."', '".$row2."', '".$row3."'); ";\\These are 2 lines to be
mysql_query($insert_into_MySQL ); \\added to the while loop
?>

Categories