This script works through a free webhosting service, but not the paid hosting service I have via iFastNet. I use cpanel to create databases and etc.
Here's my script for registering new users in my database. It is communicating with files in my android studio project.
<?php
$con = mysqli_connect("localhost", "my_username", "my_password", "userdatabase_name");
$email = $_POST["email"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM users WHERE email = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $email, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $id, $username, $email, $password);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["username"] = $email;
$response["password"] = $password;
}
echo json_encode($response);
?>
Not sure if any of you have experience with this. I'm assuming it's the web hosting service. Hopefully one of you have had this problem, and can provide a simple solution.
It's important to bring up that I can connect to the database via MYSQL Workbench, and I'm able to insert data into my table there. I just can't insert data via the simple user registration I created for an android application. It worked via a free web hosting service though.
-Also, before anyone mentions it, my user has all privileges for the database on the paid web hosting service.
Related
I'm making an Android login feature. Originally, it was made using localhost, but it is re-created with free hosting because it wants to have an external connection.
I uploaded php over FTP and changed url, which used to be localhost, to a free hosting url.
final static private String URL = "http://10.0.2.2:80/login.php";
to
final static private String URL = "http://stock123.dothome.co.kr/login.php";
When using a local host, it was run as an AVD, and when using a free hosting url, it was run as an Android device.
When using a local host, the login is executed without any problem, but after changing url, an error occurs below.
org.json.JSONException: Value DB of type java.lang.String cannot be converted to JSONObject
I don't know where the problem is.
This is login.php
<?php
$con = mysqli_connect('mirpasec.ddns.net:50106', 'extern_user', 'extern_pass', 'extern_user');
if (!$con)
{
echo "MySQL 접속 에러 : ";
echo mysqli_connect_error();
exit();
} else {
echo "DB connect success";
}
$userID = $_POST["userID"];
$userPassword = $_POST["userPassword"];
$statement = mysqli_prepare($con, "SELECT * FROM USER WHERE userID = ? AND userPassword = ?");
mysqli_stmt_bind_param($statement, "ss", $userID, $userPassword);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $userPassword, $userEmail, $userName, $userPhone);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["userID"] = $userID;
$response["userPassword"] = $userPassword;
$response["userEmail"] = $userEmail;
$response["userName"] = $userName;
$response["userPhone"] = $userPhone;
}
echo json_encode($response);
?>
I don't think there is a problem with json code because it worked normally when using the local host. There seems to be a problem connecting to the DB. Is there anything else I need to add when using FTP?
Value DB of type java.lang.String cannot be converted to JSONObject
I think the issue is you are not getting your JSONObject because somehow you didn't reach your backend api .
please check if you have necessary permission in your AndroidManifest.xml file. You need to add the following permission to grant your application to use the internet.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I'm trying to create a basic Register and Log in Application by following this tutorial: https://www.youtube.com/watch?v=JQXfIidfFMo&t=193s and because the code in Android Studio has shown 0 Compile Errors I've decided not to put that here.
What's wrong
Many users have reported in the comments section both old and new that they could not get their information sent to their database so any help would be greatly appreciated. The user inputs their 'name', 'username', 'password', and 'age' to register and this information is to then sent to my database hosted on 000webhost. Everything has been followed to the T.
The name of the table is called 'user'.
Here is the code to my Login.php file
<?php
$con = mysqli_connect("localhost:3306","id7390457_user","abcd1234","id7390457_data");
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ssis", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $user_id, $name, $age, $username, $password);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["name"] = $name;
$response["age"] = $age;
$response["username"] = $username;
$response["password"] = $password;
}
echo json_encode($response);
?>
Here is the code to my Register.php file
<?php
$con = mysqli_connect("localhost", "id7390457_user", "abcd1234", "id7390457_data");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO user (name, age, username, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "ssis", $name, $age, $username, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
I have been under the impression that the line
$con = mysqli_connect("localhost", "id7390457_user", "abcd1234", "id7390457_data");
Is followed by using ('hostname', 'username', 'password', 'nameofdatabase')
However as I am doing this from my Mac so my host is 'localhost'
My DB Name is: id7390457_data
My DB User is: id7390457_user
I've tried using 'localhost:3306'
I've tried just 'user' and 'data'
I've watched YouTube videos, visited websites in other languages and none have seemed to provide any advise.
I've deleted multiple databases starting from scratch but I still face the issue whereby my information isn't being sent to phpMyAdmin on 000webhost.
My webhost doesn't have an index.html page but I don't think that this would be the issue.
Thank you
Any help would mean the world to me. Thank you wish you well!
Hi and welcome to StackOverflow!
You are right, the mysqli_connect expects the host, user, pass and db name in that order, so if the database is running and the values are correct as you said, this looks OK.
However on this line, note the second argument ssis:
mysqli_stmt_bind_param($statement, "ssis", $name, $age, $username, $password);
It states the type of the parameters, s for string and i for integer. It looks like the order is messed up, because age is expected as a string and username as an integer. If you replace it with siss, I would expect it to work.
More info: https://secure.php.net/manual/en/mysqli-stmt.bind-param.php
I'm trying to get my Android Studio register app to send information to the database. I'm using a WAMP server. Below is my Register.php code that was uploaded and if anyone sees anything wrong with it could you help to amend it?
<?php
$con = mysqli_connect("localhost", "root", "", "users");
$Name = $_POST["Name"];
$DoctorNumber = $_POST["DoctorNumber"];
$Password = $_POST["Password"];
$statement = mysqli_prepare($con, "INSERT INTO users(Name, DoctorNumber, Password) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $Name, $DoctorNumber, $Password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
You can't connect your Android (or even iOS) app directly to a MysQL database. You'll have to create a webapp that exposes it, and the use a library like Retrofit to make the http calls.
I am following a tutorial of making a login register for android by tonakami TV in youtube, I encountered a problem where the android app can't update or connect to the database via LOCALHOST XAMPP. For one week I'm stuck searching all possible solution but found none. Perhaps you guys can help me with the problem of my PHP files(register.php and fetchuserdata.php), I also found out that I also have this error in the php files: mysqli_connect(): (HY000/1044): Access denied for user 'my_user'#'localhost' to database 'my_db'. I grant all privileges but still can't connect and I don't know if the problem is from the PHP files, the link to the server in android studio or of XAMPP localhost phpmyadmin. Thanks !
Screenshot of Android Studio: highlighted link
Register.php
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO User (name, age, username, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $age, $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
FetchUserData.php
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM User WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $age, $username, $password);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user["name"] = $name;
$user["age"] = $age;
$user["username"] = $username;
$user["password"] = $password;
}
echo json_encode($user);
mysqli_close($con);
?>
Instead of localhost use this: http://10.0.2.2
Reference
I want to add info to my data base MySQL from my iPhone app, through the use of the PHP code that follows:
<?php
if (isset($_GET["var1"]) && isset($_GET["var2"])){
$User = $_GET["var1"];
$Password = $_GET["var2"];
$conexion = conectdb();
$result = insertUser($User, $Password, $conexion);
}
function conectdb(){
$host=“http://myExample.com";
$bd=“pruebabd”;
$usuario=“StackOverFlow”;
$password=“thisIsMyPassword”;
$conexion = mysql_connect($host, $usuario, $password);
mysql_select_db($bd, $conexion);
return $conexion;
}
function insertUser($User, $Password, $conexion) {
mysql_query("INSERT INTO pruebabd (User, Password) VALUES ('{$User}’,’{$Password}’)”, $conexion);
mysql_close($conexion);
echo 1;
}
?>
My hosting works as a Plesk Hosting.
When I run my project, it doesn't work.
For the field $host, should I write the name of my domain? What should I write in $host?
I called to the enterprise who sold me the domain and hosting, but they told me that it is not possible to make this operation because it works as "Plesk Hosting". They told me that I need to change my hosting to CPanel Linux. Is it true?