I wrote this code to connect to a database, make some queries and send them by json
like web api
.
I want to create plugin that will work like a WEB API. Then upload it on WP modules on internet and people may install them on their sites to connect my android App. Anywhere.
This is my php
code (include db_connect and sending json and respond to my Android App)
<?php
if(!isset($_REQUEST ['id']))
echo "id is not set";
if (isset ( $_REQUEST ['id'] )) {
$id = $_REQUEST ['id'];
$response = array ();
mysql_connect('localhost','db_admin','password') or die(mysql_error());
mysql_select_db('db_name') or die(mysql_error());
$sql= "select * from employee";
$run=mysql_query($sql);
if (mysql_num_rows ($run) > 0) {
while ($row= mysql_fetch_array($run)){
$product = array ();
$product ["id"] = $row [0];
$product ["name"] = $row [1];
$product ["salary"] = $row [2];
$response [] = $product;
}
echo json_encode ( $response );
}
}
?>
My manager wants me not to use the default Woocommerse WORDPRESS API
,so I have to create new plugin. and want to know how can I convert it to standard module?
Is it possible at all ?
If I was you I will created like this:
First off would create handler for the request, either via ajax (im not sure if this way is supported in android ) like this;
Note: there is an
exit;
you always need to terminate the application to be able to get the json, otherwise the output it will be 0With this now we have an access point to data and validation; and it looks a bit more clean.