Connect android app to an External MySQL database using PHP - php

I have created a login_activity for my android app. I've gone through quite a few tutorials on how to fetch data from an external MySQL database. I have access to this external MySQL database. I've followed one of them to write the following PHP code which will fetch my data from the server for my login_Activity:
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
$dbhost="localhost";
$dbuser="";
$dbpass=""
$connect=mysql_connect($dbhost,$dbsuer,$dbpass) or die("database selection error");
$username=$_POST['username'];
$password=$_POST['password'];
$query=mysql_query("SELECT * FROM app_users_parent where Email='$username' AND Password='$password');
$num=mysql_num_rows($query);
if($num==1) {
while($list=mysql_fetch_assoc($query)) {
$output=$list;
echo json_encode('$output');
}
mysql_close();
}
?>
I understand I've got to place this php file onto the same server. But I don't really understand how to do that? Should I use something like FileZilla for FTP or what?
I could not find any tutorial on the net which shows how and where to save this php file.
If you need my Java code where I'm connecting to this file,I could give it to you.
(P.S.- I just started with android so not really a pro out here. Please respect that )

How to connect Android with PHP, MySQL tutorial definitely help you. Good luck.

Yes, you have to upload your .php file onto your server (wich contain the MySQL database).
Then, in you android class, create an asynctask class. That aynsctask class have to request your phpfile an get the json.

Related

Having trouble connecting to neo4j with neo4jClient Everyman

I am creating a project using php in eclipse and neo4j. I am having problems connecting though. This is the code I have written to connect to the database.
<?php
include("neo4jphp.phar");
use Everyman\Neo4j\Client;
$neo4jClient = new \Everyman\Neo4j\Client('https://127.0.0.1:7474',7474);
$neo4jClient->getTransport()
->setAuth('username','password')
->getTransport()->useHttps();
and here is the link to show the error I am having.
Link: https://selene.hud.ac.uk/irr/connect.php
According to my research this should be correct. I am also using this video to help me with the connection: https://vimeo.com/125334699
Some guidance in the right direction using my code will be appreciated.

Database connection script works in PHP 5.3 but not in 5.5

I have developed a web based member management tool for my sailing club few years ago and used some free script (a class) that handles database connection and operations on database. The class (connection.php) is invoked by the following code in a required file "settings.php":
$db = new db ( DBUSER, DBPASS, DATABASE, HOSTNAME );
and then is used throughout the site for handling database related requests. It is working fine in PHP 5.3 but due to some security concerns the site has to be upgraded to min 5.4. Unfortunately the script in PHP 5.4 fails to connect to database. I will include the script below (for brevity I show only couple of functions in the class that should give the idea):
// ==================================================================
// The Main Class
class db {
// ==================================================================
// DB Constructor - connects to the server and selects a database
function db($dbuser, $dbpassword, $dbname, $dbhost)
{
$this->dbh = #mysql_connect($dbhost,$dbuser,$dbpassword);
if ( ! $this->dbh )
{
$this->print_error("<ol><b>Error establishing a database connection!</b><li>Are you sure you have the correct user/password?<li>Are you sure that you have typed the correct hostname?<li>Are you sure that the database server is running?</ol>");
}
$this->select($dbname);
}
// ==================================================================
// Select a DB (if another one needs to be selected)
function select($db)
{
if ( !#mysql_select_db($db,$this->dbh))
{
$this->print_error("<ol><b>Error selecting database <u>$db</u>!</b><li>Are you sure it exists?<li>Are you sure there is a valid database connection?</ol>");
}
}
When trying to display a page in PHP 5.4 I get the following output:
dbh = #mysql_connect($dbhost,$dbuser,$dbpassword); if ( ! $this->dbh ) { $this->print_error("
Error establishing a database connection!
Are you sure you have the correct user/password?
Are you sure that you have typed the correct hostname?
Are you sure that the database server is running?
"); } $this->select($dbname); } // ================================================================== // Select a DB (if another one needs to be selected) function select($db) { if ( !#mysql_select_db($db,$this->dbh)) { $this->print_error("
Error selecting database $db!
Are you sure it exists?he rest of the pages
Are you sure there is a valid database connection?
My aim is to rewrite the class db without touching the rest of the pages.
I was trying to use mysqli instead of mysql but that made no difference. I'm using mainly procedural php in my programming and am not to good with object oriented.
My question is: what approach should I take to solve my problem. Could you put me in some direction...
This function appears to be deprecated as of PHP 5.5
http://php.net/manual/en/function.mysql-connect.php
If you have access to the php.ini file after the upgrade to 5.4 I would check to make sure the extension you are trying to use is unchecked in the windows extensions settings. If you pay for hosting, their tech support may be able to help if you have a lot of patience.
Thanks everybody for your contributions.
I've identified a simple (but major) problem with file connection.php containing Class db. It was using short version of php identifier: <? rather than <?php, which was causing fatal error of not finding the Class.
With this out of the way I am back to my original issue of deprecated functions not working correctly in php 5.5 but need to do more research before (eventually) asking another question.
Thanks again.
#Qchmqs Jan 8 at 7:28
you should be using PDO anyway
No!
You should be using PDO OR MySQLi
Balance please! there are plenty of well learned folk who advocate MySQLi for some situations, its a matter of judgement etc.

samba web client - setup connection in php script

I have to establish connection with Samba server from my php script in order to download some files into my local server.
Actually its first time I have heard of something like Samba so I Tried to look for a opensource code that I could make use of.
Here it is what I have found: First class - smbclient.php and I tried code posted on the page:
<?php
require_once ('smbclient.php');
$smbc = new smbclient ('//10.0.1.1/example', 'exampleuser', 'examplepassword');
if (!$smbc->get ('path/to/desired/file.txt', '/tmp/localfile.txt'))
{
print "Failed to retrieve file:\n";
print join ("\n", $smbc->get_last_stdout());
}
else
{
print "Transferred file successfully.";
}
?>
Adjusting it into my needs ( server, user, password), all i got is
Failed to retrieve file:
Fatal error: Call to undefined method smbclient::get_last_stdout()
Then I found out about smbwebclient.php project which looks awesome and can be found here.
And this class looks good but the problem is that I have no idea how to use it. Can anyone post it example connection or link to tutorial?
To get files from a samba server, you can try to use a smb wrapper, like the one here but changing the deprecated splits with explodes. Then you can include your php file using this code:
include_once('smb.php');
include( 'smb://user:password#server/folder/file.php');
Reviving old thread but I think I found the solution, the code must have changed since the pot was made on the blog:
print join ("\n", $smbc->get_last_stdout());
should now be
print join ("\n", $smbc->get_last_cmd_stdout());

php web service with json

I am new to PHP & JSON, and based on a tutorial I made a simple web service which returns the contents of a table of a mysql db.
The output is in JSON and the database caracter set is UTF-8. my problem is that when I try to run this it throws me a 404 error, and doesn't show me where the error is. Any idea what might be wrong?
The PHP file is the following:
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
$link = mysql_connect($mysql_host,$mysql_user,$mysql_password) or die("cannot connect to the DB");
mysql_select_db($mysql_database,$link);
$query = "select...";
$result=mysql_query($query) or die (mysql_error("error "));
$num=mysql_numrows($result);
$rows=array();
while($r=mysql_fetch_assoc($result)){
$rows[]=$r;
}
echo json_encode($rows);
?>
Any Idea? Thanks in advance
Add following line before json_encode()
header('Content-type: application/json');
That should solve your problem.
In this , I have written my php web service code.
follow this.
hope it will help you.
link :
http://stackoverflow.com/questions/12702128/json-parsing-in-iosphp
Are you sure you want use JSON? You can also use SOAP-NuSOAP and WSDL for creating a web service. See, for example,
Create a PHP Web Service in 5 Minutes Using PHP SOAP and WSDL Technology and NuSOAP.

Insert PHP to mySQL database not working online (via flash application)

So, here is the question. I have a small flash application that sends some variables to a php script (via POST), and that php script sends them to a mySQL database.
This works fine when I'm testing my flash offline, I go to phpmyadmin, and the registry is done. When I upload the swf to a online html, this stops working. It no longer creates another registry in the database. I have no ideia why this happens.
I read a bit about it and found out about cross domain policy, and thought it might be the problem. Thus I made a small .xml file and uploaded it to both servers (the one the swf is on, and the one that has the database), and its still not working.
Here is my crossdomain.xml file (named that way):
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>
And the php (named insert.php), that is on the same server as the database:
<?php
//connect to the local MySQL
$connect=mysql_connect("localhost", "DBName", "DBPass");
//select your database
mysql_select_db("DBTable");
//query the database
$query="INSERT INTO results(Nome,Email,Idade,Profissao,Pais)
VALUES
('$_POST[nome]','$_POST[email]','$_POST[idade]','$_POST[profissao]','$_POST[pais]')";
//$result=mysql_query($query);
if (!mysql_query($query,$connect))
{
die('Error: ' . mysql_error());
echo "InsertOk=NotOk";
}else{
echo "InsertOk=Ok";
}
mysql_close($connect)
?>
Thank you!
Marco Roberto
Apart from obvious security flaws, try to
"INSERT INTO results(Nome,Email,Idade,Profissao,Pais)
VALUES
('{$_POST['nome']}','{$_POST['email']}','{$_POST['idade']}','{$_POST['profissao']}','{$_POST['pais']}')";
Basicly add {} and ''.
EDIT:
Nevermind that part. Noticed it's not relevant.
I believe you have to do the call to your own site, and use CURL to pass $_POST-values to the other domain.
Try this answer
Even though this is for javascript, I belive the same rules apply for your swf file.

Categories