android application on a device connect to a php script - php

my english is not that good so i will try to explain my problem:
i have a group of .php files in a server (external webserver). i installed the android application on my tablet running android 3.2.1 and i need to:
1) call a php file that do the conection to the db (mysql database)
2) do some stuff on the db (always using the php files that do the work
i have download the tutorial from http://download.androidhive.info/download.php
i have tried in all the ways to make it work for my case but without any result.
maybe i'm just missing something...something really stupid (as always) that is making my connection fail. thansk in advance and i would love some advices (not just for the android java files but also for the php files)
<?php
/*
* All database connection variables
*/
define('DB_USER', ""); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "androidhive"); // database name
define('DB_SERVER', ""); // db server
?>

Please, check this out:
http://ugirusgiarto.wordpress.com/2011/10/27/json-php-mysql-with-asynctask-progressdialog/
I think it's better to make the tests on localhost before, with WAMP or XAMPP.
And delete your database connection data before someone get into your database.

place all the files under the same folder and direct your link to that folder (in case its under /var/www simply write the address ommiting that path) then replace the :
require_once DIR . '/db_connect.php';
in - require_once 'db_connect.php';
set files permissions to 777 (just for tutorial, in real life you should grant permissio
that should do the trick...

Related

php & mysql setup on Linux

I am learning Linux, MySQL (setup) and php, all in one go. Slowly getting the hang of it but I am trying to build the structure for my site and I can't seem to get the connection to my database which I have moved to /var/www/html/site_01/_database/ and the page that is trying to connect to is located at /var/www/html/site_01/_html/ I have been on W3Schools and quite a few other sites giving tutorials on php and everywhere states $servername = "localhost"; but I am still unsure if I need to edit the config files of php or MySQL to have the dir of my database added?
Thanks in advance!
I see that you are probably mistaking URL/URI with phisical path.
You do not need to have your database in any particular directory. As long as its up and running on the same machine as your application server (e.g.:MAMP,XAMP,pure apache,etc) you will be on localhost.
All you need to do is connect to your database on your PHP algorithms, with the correct driver for your DBSM on your project and imported.
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
with the correct mysql_user and mysql_password

Why does only one of my SBS 2011 IIS Websites connects to the MySQL but the other one does not?

I have multiply websites configured and setup on a SBS 2011 virtual machine.
The default wwwroot website connects to MySQL without any trouble with the host set to 'localhost'. But the second website that has it's own seperate directory location along side the wwwroot directory can not access the MySQL Database with 'localhost' as its host connection.
Anyone have any ideas why the default site connects to the MySQL Database but the second website does not?
Okay here is my directory structure.
inetpub
../websites
../../website1
../wwwroot
<?php
$db_username="username"; $db_password="password"; $database="database"; $db_host="localhost";
mysql_connect("$db_host", "$db_username", "$db_password") or die("cannot connect"); mysql_select_db("$database")or die("cannot select Database");
?>
When ever I run this code in the wwwroot directory it works but not when I execute this code in the website1 directory.
UPDATE!!!
So it seems that it is not my connection to the MySQL database that is the problem it is the include php function.
So my problem is now why does the php include function work within the wwwroot folder and not the website1 folder?
Thanks
Foo
Okay I think I have solved the problem myself. I have been programming with php for several years now and have just realized that I need to follow and remember all the new changes.
Too many mistakes for me to list but thanks anyway people for reading my problem.
Ensure all PHP code is encased in
<?php ?>
and not
<? ?>
It might work on one server but it won't work on servers running the latest version of PHP 5.6

How to write MySQL DB single connection string for my website application

I have a website built using HTML,JS,PHP. I use Linux OS and apache2 server and MySQL as a back end. To connect to the MySQL server I have created connection in each PHP page,But I want this connection string to be written in a single file(Like web.config in Visual studio),so that I can change the connection string easily whenever it is necessary.I have installed PHPMYADMIN and I can see couple of config files in that folder.But I do not know where I need to write this connection string?
Or shall I create a PHP file with the connection string and include() this file in every other PHP files and create the instance of the connection?
I am not using any IDE for building the website.
Any help is greatly appreciated.
Thanks a lot.
Create a PHP file where you connect to the SQL server and save the connection in a variable. Include this file everywhere you need the connection.
Pseudo example
init.php
$db_connection = connect_to_db($server, $username, $password, $db);
other_files.php
include "init.php";
execute_query("query", $db_connection);
Create a PHP file with connection setting and just include it in all pages at the top.
http://php.net/manual/en/pdo.connections.php Here is an exampe with PDO
Just put that in a php file and include it where is needed.

php can connect remotely to mysql but not locally

UPDATE:
I tried having my php file in windows and connect to the mysql server remotely in linux using the code below
mysql_connect('10.128.xx.xx', 'jflim', 'helloworld');
this works! but when i tried uploading the same php file in linux(server) and changing the connection string to
mysql_connect('localhost', 'root', '');
its not working.. its also not showing any error message. php codes are working fine without database connection. Im not sure what the problem is now. many thanks
If you upload the .php file in the server where the mysql server is running you should use 'localhost'. But if the script is located in another machine replace the 'localhost' with an ip address or url (www.example.com).
Apart from that, make sure you use the right username and pw.

Connect to MySQL with hashed password?

I was wondering (and used Google with no clear result) if there is any way to connect to a MySQL database with PHP using a hashed password. Say I have the following:
Password (plain): 'foobar'
Password (sha1): '8843d7f92416211de9ebb963ff4ce28125932878'
Now I would like to connect to MySQL like this (using the mysql_* function as example, I'm using PDO though):
$db_link = mysql_connect ( 'localhost', 'user', '8843d7f92416211de9ebb963ff4ce28125932878' );
I this possible? Have anyone done this before?
Then the "hash" would be the password. What would be the benefit?
The short answer is no.
But, just wondering... what is your real concern? Someone hacking into your server and discovering your password?
the usage case would be having multiple developers editing the .php file that contains the sql connect password that you might not want them to know.
I think one solution would be to move the connect statement out to a file like so, make sure you don't have a $password variable though cause someone could just call it and print it out later in their .php file
mysql.php
<?php
mysql_connect('db.cs.dal.ca','user','password');
#mysql_select_db($database) or die( "Database Error ".mysql_error());
?>
and only give your self rw------- permissions to the mysql.php file, then in all of your group accessible .php files you can just include that file to evoke a connection.
index.php
<?php include("mysql.php") ?>
<!-- some web content -->
<?php mysql_close(); ?>
and give your developers group rw-rw---- permissions on all the other .php files, as long as the owner of the mysql.php file can read it should executed on the php server..... i think.
you can also exclude mysql.php from git for example, and have developers run their own local copy of a DB with their own mysql.php file and just provide a stripped down copy of your production database for local development and testing
Simple answer is "You can't."
I know what you are trying to accomplish: You are probably on some shared hosting plan and cannot put your config file above the html folder.
Stefan is thinking that a hacker would just be hunting for the config file and wants to make him have to work for the info. Once the hacker realizes he needs more info, he has to crack the site a second time.
This has nothing to do with a table of usernames & passwords. This is for the MySQL config file.

Categories