我想连接MySQL数据库和它没有工作 我试过这段代码:如何使用php页面代码连接到MySQL数据库?
public void loginPost(View view){
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
try{
String link="http://mwssong.esy.es/android/Login.php";
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
if(sb.toString()=="admin")
new AdminScreen();
else if(sb.toString()=="Customer")
{
Intent myIntent(view.getContext(),AdminScreen.class);
startActivity(myIntent);
}
else
status.setText(sb);
}
catch(Exception e){
status.setText("Exception: " + e.getMessage());
}
}
但它总是给我的状态提起异常空 和意图不工作, ECLIPSE在所有拒绝此说明没有解决建议:
Intent myIntent(view.getContext(),AdminScreen.class);
startActivity(myIntent);
和PHP代码为:
<?php
$db = mysqli_connect('mysql.hostinger.ae','u641845309_ur','q1p0w2o9','u641845309_song');
// username and password sent from Form and protect MySQL injection for Security purpose
$username=mysqli_real_escape_string($db,$_POST['username']);
$password=mysqli_real_escape_string($db,$_POST['password']);
$sql="SELECT * FROM customer WHERE UName='$username' and Password='$password'";
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$result=mysqli_query($db,$sql);
// If result matched $myusername and $mypassword, table row must be 1 row
if($result)
{
while($row = mysqli_fetch_array($result)) {
// Redirecting To Other Page
if(strtolower($username)=='admin')
echo "Admin";
else
echo "Customer";
}
}
else
{
echo "Your Login Name or Password is invalid";
}
?>
这段代码没有连接到mysql数据库,它连接到一个php页面。 – Shadow
对不起,我忘了php代码: 这是我的: –
我把它放在帖子中 –