same result in number of rows in mysql with php - android

I work at android application that connect with database in server
I need to check log in by user name and password
If the user name and password found in database the user found
I add in table log in usernames and passwords but the result of code is no
$name = $_POST["user_name"];
$password = $_POST["password"];
$result = mysql_query("SELECT user_name,password from login WHERE user_name = '$name' AND password = '$password'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows == 1) {
$json = array("result"=>"yes");
} else {
$json = array("result"=>"no");
}
header('Content-Type: application/json');
echo json_encode($json)
I add in table login usernames and passwords but the result of code is no if username and password founded or not
what is the problem in this code ??

This can be due to several reasons:
The mysql_query is returning an error. I'd recommend to check the returning result with:
if ($result === FALSE)
echo "I have an error in my MySQL sentence\n";
It's not mandatory, but I don't know whether you have more than one opened MySQL socket. That's why it's always a good idea to specify the link id to the mysql_query function, e.g.:
$mylink = mysql_connect($host, $login, $password);
$result = mysql_query("SELECT ...", $mylink);
It's always a good idea to test the sentences you're testing in your code manually. So I'd put echo "SELECT user_name,password from login WHERE user_name = '$name' AND password = '$password'\n";, then copy it, manually connect to the MySQL server and execute it and see whether it's returning anything (in case it returns something, that means that the error is in the code).
It's not a debugging tip, but I'd recommend always encrypting the passwords in the database. So when you insert a login in the database, instead of inserting ('$user', '$pass'), better insert ('$user', MD5('$pass')), for a better security. For the SELECTs it's the same: SELECT * FROM table WHERE login = '$user' AND pass = MD5('$pass').
Hope this helps!

Related

Get data from Firebase Database with Specific node after successfull Authentication

I have used FirebaseAuth for Login purpose in application. While createUserWithEmailAndPassword I am creating the same user in FirebaseDatabase with some additional fields.The structre is
[{
"email":"abc#gmail.com",
"password":"xyz",
"name":"sachin",
"address":"Pune",
"contact":"1234567890"
},
{
"email":"pqr#gmail.com",
"password":"def",
"name":"Ashay",
"address":"Pune",
"contact":"1234577777"
}]
Suppose after successfull Login of user Sachin with email abc#gmail.com and password xyz I want the address and contact from database.How to get that values ?
For future reference if you want to get a certain information from DB using UID
//To get logged in user information
var user = firebase.auth().currentUser;
var uid = user.uid; //get the UID from the current user
According to me, you should use firebase uid as index for storing data. When user authenticated you will get user's uid, on this basis you can access user's data. I hope it will work.

Google App Engine - Datastore - Get an entity, not by it's key

I'm building an Android app, and I'm using Google App Engine to store user's data. I want users to be able to connect from other devices to their account, but I could not figure how.
The users table (kind) has 4 properties: id, email, nickname and level:
I have read this:
https://cloud.google.com/appengine/docs/standard/java/datastore/creating-entities
It's written their that I can only get entities by their key, and it's a problem for me, because in a new device, I can only get the user's email, not the key and Id. I need to get the key and id by their email.
Is this possible?
If it's not, is their any other solution?
You just need to do a simple query. What language are you using in the GAE backend? For example, in Python, you would do something like:
def get_user_by_prop(prop, value):
this_user = User.query(getattr(User, prop) == value).get()
return this_user
Judging from the link in your question, I assume you are using Java? Here are the docs for a query: https://cloud.google.com/appengine/docs/standard/java/datastore/retrieving-query-results
where they use the example:
Query q =
new Query("User")
.setFilter(new FilterPredicate("nickname", FilterOperator.EQUAL, "billybob"));
PreparedQuery pq = datastore.prepare(q);
Entity result = pq.asSingleEntity();
What is stored at the id property? Does it have some meaningful value or just random unique number to be used as a unique identifier?
It seems like you can design your database differently so the email address would be your unique identifier. In that case, you will have a User table contains the following properties:
email (your unique identifier), nickname and level.
That way you will be able to use the following code snippet:
Key userEmail; //Get user's email
Entity user = datastore.get(userEmail);
Regardless of that, you are still able to access your entity without the need for the entity's key, by using a query. That way you won't be using the entity's key in order to get its instance but you would rather get the desired entity by using the given property value and finding the matching entity with that property value.
The query would look something like that:
String userEmail; //Get user's email
Filter propertyFilter =
new FilterPredicate("email", FilterOperator.EQUAL, userEmail);
Query q = new Query("User").setFilter(propertyFilter);
PreparedQuery pq = datastore.prepare(q);
try {
Entity user = pq.asSingleEntity()
//Same as Entity user = datastore.get(userEmail) mentioned above
} catch (TooManyResultsException e) {
// If more than one result is returned from the Query.
// Add code for dealing the exception here
}

MySQL to Sqlite sync from PHP to mobile app (iOS/Android)

To sync a mysql database with sqlite database using a PHP web app, I am going to
create a "view" mirror of SQLITE tables in MySQL
select changed data on mirror "view" in MySQL
use mysqldump to dump the sql for sqlite and save it in a file on server
download that file from mobile
import data directly to sqlite
another approach would be not using a middle "view" table and dump data using "SELECT" or mysqldump and parse it on the device or server, so it would be readable by sqlite
is there any disadvantages in creating a mirror "view" for each table that SQLITE needs on client devices? or is there another 'standard 'way to sync these 2 which i did not find on the internet?
Edit: after searching and coding some more finally i decided to go like this:
build "insert into ..." statement for sqlite from PHP on the server and save it to a zipped file
download the file on mobile and insert extracted sql directly to SQLITE
here is the php code for generating mysqldump like output:
/** key [SQLITE column name] => value [MySQL select column] */
$table_structure = array(
'id' => 'id',
'title' => 'title',
'type' => 'COALESCE(type, "")', /** u need to wrap any column that is nullable like this **/
'flags' => 'COALESCE(type, "")', /** u need to wrap any column that is nullable like this **/
'view_field' => 'view_field',
'view_year' => 'view_year',
....
....
'updated_at' => 'UNIX_TIMESTAMP(updated_at)');
$into_query = '';
$into_values = '';
$indexer = 0;
foreach ($table_structure as $sqlite => $mysql) {
$into_query .= (($indexer !== 0)? ',':'').$sqlite;
$into_values .= (($indexer !== 0)? ',",",':'').'"\'",'.$mysql.',"\'"';
$indexer++;
}
$output_filepath = "some where nice";
$query = 'SELECT CONCAT("INSERT INTO '.$table.'('.$into_query.') VALUES(",'.$into_values.',");") FROM '.$table.' ' ... "INTO OUTFILE $output_filepath CHARACTER SET utf8";
I hope it's a good approach and may help someone some day :)
Approach 1: using sqlite db file
whatever step you have mentioned in question is correct. this approach used when you have to send lots of data(many tables) from device to web/php vice versa.
its takes time in slower internet connection as well in 2G. in 3G its fast(its also depends on whatever code you have written at mobile/PHP to upload & download file).
make sure database file size is small if it large then make zip file which include database file.
Approach 2: make using json (SELECT *) parse in device
this one is very long process when you have lost of table which have lots of fields. you have to get data from table one by one, send it to server/php its send back data after process. then you have to again insert data in device table.
this is also depends on INTERNET as well you code. its too slow for devices and take more time.
common things in both approach send/receive data only when its needed or on those which require for devices/PHP. do not send unused data to work fast.

Android pull to refresh listview data duplication while fetching data from localhost php and mysql server

my php code for fetching data from database.. can anybody tell me how can i load data into my android pull to refresh listview only new data
$sql = "SELECT * FROM hotelbook";
$res = mysql_query($sql);
$arr = array();
while($row = mysql_fetch_assoc($res)){
$arr[] = $row;
}
die(json_encode($arr));
For that you have to use Recyclerview.
Next to that is Example.
As the above Example link will describes you all the things step by step.

optimizing apache and mysql for android clients requests

I would like to know which apache/httpd parameters a relevant to set, if I expect a high number of connections from different IPs, getting same ammount of data in the same time. As example: I would have 10000 mobile devices that try to get data from server in the same time. The dataset is about 100k big and is generated by a php script. Each call make a query to the database. How apache(and mysql) should be configured to stay alive during this requests?
<?php
$con = mysql_connect( 'localhost', 'root', 'pass') or die('Could not connect' );
mysql_select_db('mydb', $con) or die('Could not select database.');
$result = mysql_query("SELECT * FROM results");
mysql_close($con);
$items = array();
while($row = mysql_fetch_array($result)){
$items[] = array(
's' => $row['sport'],
....
);
echo json_encode($items);
}
Regards!
Dimitri.
First of all use mysqli API and not mysql (http://it2.php.net/mysqli).
Then you MUST use the bind param technique (http://it2.php.net/manual/en/mysqli-stmt.bind-param.php) beacuse the generated query have so much better performance because mysql deosn't have to rebuild the eexecution plan of the query but only reexecute it.
Server side pay attention to doesn't slow MyISAM driver but InnoDB. If you can manage the table structure adopt well known normalization practice : http://en.wikipedia.org/wiki/Database_normalization.
Also do the code inside your while in async way so u can "parallelize" the execution of the code.

Categories

Resources