I am working on android app in which a listview with some text retrieve from server The source is here source link. This is my php code
<?php
require_once("dbConnect.php");
$sql = "SELECT image,fullname,location from uploadfinding";
$res = mysqli_query($conn,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result, array(
"image"=>$row[0],
"fullname"=>$row[1],
"location"=>$row[2]));
echo " over";
}
echo json_encode($result);
mysqli_close($conn);
?>
and this is my json response
Connected successfully over over over[{"image":myurl\/uploadfinding\/uploads\/2016-04-25 06:38:051461584281226.jpg","fullname":"adi","location":"fgh"},{"image":myurl\/uploadfinding\/uploads\/2016-04-25 06:38:201461584297706.jpg","fullname":"adi2","location":"fgh2"},{"image":myurl\/uploadfinding\/uploads\/2016-04-25 06:45:441461584739479.jpg","fullname":"adi23","location":"cn"}]
I have lots of things tried but result is none.the complete source is here:
I am not posting here any java file bacause this example is worked fine for this url:
http://api.androidhive.info/json/movies.json
But when i replace this with my url:
http://myurl/PhotoUpload/getAllImages.php
It return only empty activity.
Try This
<?php
header("content-type:application/json");
require_once("dbConnect.php");
$sql = "SELECT x1,x2,x3 from table_name";
$res = mysqli_query($conn,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result, array(
"x1"=>$row["x1"],
"x2"=>$row['x2'],
"x3"=>$row["x3"]));
echo " over";
}
echo json_encode($result);
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($result));
fclose($fp);
mysqli_close($conn);
?>
replace your url "http://myurl/PhotoUpload/getAllImages.php" with "http://myurl/PhotoUpload/results.json"
Just to clarify:
from within your app, you can simply refer to the emulator as
'localhost' or 127.0.0.1.
or
If you are running with real device means pass correct url like
http://xxx.xxx.xxx.xxx/android/php_server/register.php
Edit:
Try compare your url,
myurl/uploadfinding/uploads/2016-04-25 06:38:051461584281226.jpg"
with
"http://api.androidhive.info/json/movies/1.jpg" this.
At your response, " double quote is missing at front of image tag
pass image link like,
"your_link"
Related
guys currently I build a web site using PHP. I connect my website to the database using web services (url). The system is run as well. Then, I separate the web services by creating other PHP files, place the web services and include the PHP file to my web site. This is also running well. Below is the code:
add_factory.php
<?php
include("../../config/check.php");
if(isset($_POST['Submit']))
{
$Fac_ID = $_POST['Fac_ID'];
include("../../api/api_add_factory.php"); //include web services 1
if(empty($queryt)){
include("../../api/api2_add_factory.php"); //include web services 2
if(!empty($json2)){
header("Location:factory.php");
}else{
echo "
<script>alert('Something wrong, please try again')</script>
<script>window.location = 'factory.php'</script>
";
}
}
else{
echo "
<script>alert('The factory you want to add is already exist')</script>
<script>window.location = 'factory.php'</script>
";
}
}
?>
api_add_factory.php
<?php
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/selectFactory?Fac_ID=$Fac_ID";
$data = file_get_contents($url);
$json = json_decode($data);
$queryt = $json->factoryList;
?>
api2_add_factory.php
<?php
$url2 = "http://172.20.0.45/TGWebService/TGWebService.asmx/insertFactory?fac_id=$Fac_ID&fac_name=$Fac_ID";
$data2 = file_get_contents($url2);
$json2 = json_decode($data2);
?>
When you see at "add_factory.php", There's two include. Now, I want to use only single include that can call "api_add_factory.php" and "api2_add_factory.php". But I dont know how to do that since the "include" at "add_factory.php" is at different position.
Can anyone knows how to merge both include file into one and where i need to pu the include file at "add_factory.php"?
My suggestion would be to rewrite the contents of api_add_factory.php and api2_add_factory.php as functions.
This would allow you to control how and when they are called by the piece of code that includes them, rather than being executed immediately when the include is called.
Also, as a general rule of thumb, includes should all be called at the beginning of your file because otherwise they can become very difficult to keep track of.
So, I would say you can do something like this:
api_add_factory.php
<?php
function callApi1()
{
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/selectFactory?Fac_ID=$Fac_ID";
$data = file_get_contents($url);
$json = json_decode($data);
return $json->factoryList;
}
api2_add_factory.php
<?php
function callApi2()
{
$url2 = "http://172.20.0.45/TGWebService/TGWebService.asmx/insertFactory?fac_id=$Fac_ID&fac_name=$Fac_ID";
$data2 = file_get_contents($url2);
return json_decode($data2);
}
add_factory.php
<?php
include("../../config/check.php");
include("../../api/api_add_factory.php"); //include web services 2
include("../../api/api2_add_factory.php"); //include web services 2
if(isset($_POST['Submit']))
{
$Fac_ID = $_POST['Fac_ID'];
$queryt = callApi1();
if(empty($queryt)){
$json2 = callApi2();
if(!empty($json2)){
header("Location:factory.php");
}else{
echo "
<script>alert('Something wrong, please try again')</script>
<script>window.location = 'factory.php'</script>
";
}
}
else{
echo "
<script>alert('The factory you want to add is already exist')</script>
<script>window.location = 'factory.php'</script>
";
}
}
Actually, now that I am taking a look at my answer, I can see a possibility for a further improvement: since the contents of api_add_factory.php and api2_add_factory.php are actually the same apart from the url that gets called, you can do something like this:
api_add_factory.php
<?php
function callApi($url)
{
$data = file_get_contents($url);
return json_decode($data);
}
add_factory.php
<?php
include("../../config/check.php");
include("../../api/api_add_factory.php"); //include web services
$url1 = "http://172.20.0.45/TGWebService/TGWebService.asmx/selectFactory?Fac_ID=$Fac_ID";
$url2 = "http://172.20.0.45/TGWebService/TGWebService.asmx/insertFactory?fac_id=$Fac_ID&fac_name=$Fac_ID";
if(isset($_POST['Submit']))
{
$Fac_ID = $_POST['Fac_ID'];
$json1 = callApi($url1);
$queryt = $json1->factoryList;
if(empty($queryt)){
$json2 = callApi($url2);
if(!empty($json2)){
header("Location:factory.php");
}else{
echo "
<script>alert('Something wrong, please try again')</script>
<script>window.location = 'factory.php'</script>
";
}
}
else{
echo "
<script>alert('The factory you want to add is already exist')</script>
<script>window.location = 'factory.php'</script>
";
}
}
This way, we don't need to repeat the code of the function.
I've successfully setup retrofit 2 in android studio. The app displays the following error when doing a GET request to the mysql database via the php file.
expected begin_array but was string at line 1 column 1 path $
I've tried rewriting the php file several times but its not been successfull. Please see my current php file below.
<?php
header('Content-Type: application/json');
include('conn.php');
$response = array();
//if(isset($_GET['1'])){
//$getid = $_GET['1'];
$read = "SELECT * FROM Cbt";
$result = $conn->query($read);
while ($row = $result->fetch_assoc()) {
$cbtLog = [
$userId = $row['userId'];
$moodBefore = $row['moodBefore'];
$automaticThought = $row['automaticThought'];
$distortions = $row['distortions'];
$challengeThought = $row['challengeThought'];
$alternativeThought = $row['alternativeThought'];
$moodAfter = $row['moodAfter'];
];
array_push($response, $cbtlog)
}
echo json_encode($response);
I would like the fix whatever the error is in the php file so that the results can be displayed on the android app. Please help me diagnose and fix the problem.
Got it working
<?php
include('conn.php');
enter code here
$query = "SELECT * FROM feelingFit_Cbt";
$result = mysqli_query($conn, $query);
$json_array = array();
while ($row = mysqli_fetch_assoc($result)){
$json_array[] =$row;
}
echo json_encode($json_array);
I would like to find out how a PHP page calls another PHP page, which will return JSON data.
I am working with PHP (UsersView.php) files to display my contents of a website. However, I have separated the MySQL Queries in another PHP (Get_Users.php) file.
In the Get_Users.php, I will have a MySQL statement to query the database for data. It will then encode in JSON and be echo-ed out.
In the UsersView.php, I will call the Get_Users.php in order to retrieve the Users JSON data. The data will then be used to populate a "Users Table".
The thing is, I do not know how to call the "Get_Users.php" from the "UsersView.php" in order to get the data.
Part of UserView.php
$url = "get_user.php?id=" . $id;
$json = file_get_contents($url);
$result = json_decode($json, true);
I am trying to call the file which is in the same directory, but this does not seem to work.
Whole of Get_Users.php
<?php
$connection = mysqli_connect("localhost", "root", "", "bluesky");
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " . mysqli_connect_error() . " (" . mysqli_connect_errno() . ") " .
"<br>Please retry your last action. Please retry your last action. " .
"<br>If problem persist, please follow strictly to the instruction manual and restart the system.");
}
$valid = true;
if (!isset($_GET['id'])) {
$valid = false;
$arr=array('success'=>0,'message'=>"No User ID!");
echo json_encode($arr);
}
$id = $_GET['id'];
if($valid == true){
$query = "SELECT * FROM user WHERE id = '$id'";
$result = mysqli_query($connection, $query);
if(mysqli_num_rows($result) == 1){
$row = mysqli_fetch_assoc($result);
$arr=array('success'=>1,'type'=>$row['type'],'user_id'=>$row['id'],'email'=>$row['email'],'name'=>$row['name'],'phone'=>$row['phone'],'notification'=>$row['notification']);
echo json_encode($arr);
}else{
$arr=array('success'=>0,'message'=>"Invalid User ID!");
echo json_encode($arr);
}
}
mysqli_close($connection);
?>
You have a couple of different ways to accomplish this:
You should be able to first set the actual id and then include the Get_Users.php file like this. Notice that you should not echo out the output from Get_Users.php, instead only return the encoded json data using return json_encode($arr);:
// set the id in $_GET super global
$_GET['id'] = 1;
// include the file and catch the response
$result = include_once('Get_Users.php');
You can also create a function that can be called from UserView.php:
// Get_Users.php
<?php
function get_user($id) {
// connect to and query database here
// then return the result as json
return json_encode($arr);
}
?>
// In UserView.php you first include the above file and call the function
include_once('Get_Users.php');
$result = get_user(1);
You could also use file_get_contents(). Notice that you need to make sure so that allow_url_fopen is enabled in your php.ini file for this to work:
$result = file_get_contents('http://example.com/Get_Users.php?id=1');
To enable allow_url_fopen you need to open up your loaded configuration file and set allow_url_fopen=1 and finally restart your webserver.
You could also use curl to achieve the same result:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://example.com/Get_Users.php?id=1');
$result = curl_exec($ch);
curl_close($ch);
An ajax request could also be made to get the result. This example uses jQuery:
$(document).ready(function() {
$.get({
url: 'Get_Users.php',
data: 'id=1',
success: function(response) {
// response contains your json encoded data
// in this case you **must** use echo to transfer the data from `Get_Users.php`
}
});
});
Change UsersView.php to like this
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['CONTEXT_PREFIX'];
$url = "get_users.php?id=" . $id;
$url = $actual_link.$url;
$json = file_get_contents($url);
$result = json_decode($json, true);
This will work fine.
I am trying to add image in MAMP server table & fetch it in PHP, and create json. But I am not getting how to add image in table and again it in file. I am new in PHP scripting. Please someone provide me right direction. I have added my PHP code & also MAMP table screenshot.
PHP Code file
<?php
$conn = mysql_connect("localhost:8888","asmita","asmita123") or die(mysql_error());
if($conn)
{
mysql_select_db("EmployeeInfo");
//echo "connected";
//echo $_SERVER['DOCUMENT_ROOT'];
}
else
{
echo "not connected";
mysql_error();
}
$selectQuery="select * from Emp";
$row=mysql_query($selectQuery);
while($result=mysql_fetch_array($row))
{
//echo $_SERVER['http://localhost:8888/images/index.jpg'];
//output need in JSON format for webservice ...
$empname= $result['Name'];
$empadd= $result['Address'];
$emppho= $result['Phone'];
$emppost= $result['Post'];
$empphoto=$result['Photo'];
$jsonArray[]=array("name"=>"$empname","address"=>"$empadd","phone"=>"$emppho","post"=>"$emppost","photo"=>"$empphoto");
}
echo json_encode($jsonArray);
?>
MAMP table data
Here I added image path. Is this correct? Thanks.
HTML Code
<input type="file" name="img_file" id="imageUpload">
PHP Code
$filename = $_FILES['img_file']['name'];
$src = $_FILES['img_file']['tmp_name'];
$folder = "/images/" ;
$move= move_uploaded_file($src,"$folder/".$image);
if($move!=false)
{
$pic = "http://localhost:8888/directoryName/images" .$filename; //This variable insert into Photo column
$query="Insert or Update Query for your table";
$rslt = mysql_query($query);
}
$data=array();
$selectQuery="select * from Emp";
$row=mysql_query($selectQuery);
while($result=mysql_fetch_array($row))
{
$data[] = $result;
}
echo json_encode($data);
$result=mysql_query("select * from pointtable where Latitude between '$latitude1' and '$latitude2' and Longitude between '$longitude1' and '$longitude2' ");
$posts=array();
if(mysql_num_rows($result))
{
while($post = mysql_fetch_assoc($result))
{
$posts[]=array('post'=>$post);
}
}
header('Content-type: application/json');
echo json_encode(array('posts'=> $posts));
above code is of creating json response..i have one form from which by POST method i am getting parameter information..result is fine but it opens in diagloue box..i want to type this json response in a page...what do i do?..
{"posts":[{"post":{"id":"1","LayarType":"college","Attribution":"Daiict","Title":"CEP Daiict","Latitude":"23.3400000000","Longitude...}
i donot want this starting...{"posts":"post'}..want to start it from {id:1...}
what do i change?...
try
while($post = mysql_fetch_assoc($result))
{
$posts[]=$post;
}
header('Content-type: text/plain');
echo json_encode($posts);