这个问题可能重复,但我无法在问题中找到任何解决方案。
这是一种简单的登录方式。
基本上我从用户那里获取两个参数并将它们发送到服务器,服务器搜索数据库并返回与我发送的参数匹配的结果。如果没有匹配的结果,方法只返回null,所以我可以要求用户更正用户名和密码。
以下是我用来发送参数的代码;
@Override
protected User doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(0,new BasicNameValuePair("username",user.username));
dataToSend.add(1,new BasicNameValuePair("password",user.password));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams,CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.okanyakit.com/tez/FetchUserData.php");
User returnedUser = null;
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
if (jObject.length() == 0){
returnedUser = null;
}else {
String username = jObject.getString("username");
String password = jObject.getString("password");
String email = jObject.getString("email");
String phonenumber = jObject.getString("phonenumber");
String bloodtype = jObject.getString("bloodtype");
String birthday = jObject.getString("birthday");
String address = jObject.getString("address");
returnedUser = new User(username,password,email,phonenumber,bloodtype,birthday,address);
}
}catch (Exception e){
e.printStackTrace();
}
return returnedUser;
}
在这里,我可以将用户名和密码都放到列表中,但是当它连接到服务器时,我收到一个错误,它说我只发送了一个参数,而期望有两个。
我不认为我的PHP代码有问题,但我不确定,所以在这里;
<?php
$con=mysqli_connect("89.19.30.210","okan_tez","okan_tez","okan_tez_andro");
$username = $_POST["username"];
$password = $_POST["password"];
mysqli_close($con);
$statement = mysqli_prepare("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, $username, $password, $email, $phonenumber, $bloodtype, $birthday, $address);
$user = array();
while (mysqli_stmt_fetch($statement)){
$user [username] = $username;
$user [password] = $password;
$user [email] = $email;
$user [phonenumber] = $phonenumber;
$user [bloodtype] = $bloodtype;
$user [birthday] = $birthday;
$user [address] = $address;
}
echo json_encode($user);
mysqli_stmt_close($statement);
?>
这是调试截图;
这里是用绿色字体说的
警告:mysqli_prepare()需要2个参数,1 /home/okanyakit.com/httpdocs/tez/FetchUserData.php 在 11 行中给出>
警告:mysqli_stmt_bind_param()要求参数1为mysqli_stmt,在 /home/okanyakit.com/httpdocs/tez/FetchUserData.php 12 <中给出null / b>
警告:mysqli_stmt_execute()要求参数1为mysqli_stmt,在 /home/okanyakit.com/httpdocs/tez/FetchUserData.php 13
警告:mysqli_stmt_store_result()要求参数1为mysqli_stmt,在 /home/okanyakit.com/httpdocs/tez/FetchUserData.php 中 14 < / b>
警告:mysqli_stmt_bind_result()要求参数1为mysqli_stmt,在 /home/okanyakit.com/httpdocs/tez/FetchUserData.php 15 < / b>
警告:mysqli_stmt_fetch()要求参数1为mysqli_stmt,在 /home/okanyakit.com/httpdocs/tez/FetchUserData.php 18 < / b>
[]
警告:mysqli_stmt_close()要求参数1为mysqli_stmt,在 /home/okanyakit.com/httpdocs/tez/FetchUserData.php 29 <中给出null / b>
那么问题是什么,我该如何解决呢?请项目截止日期为两天,我还有很多问题。我对注册部分有一个类似的问题。我想如果我能解决这个问题,我也能解决这个问题。感谢。
答案 0 :(得分:1)
你不应该在这里关闭连接:
mysqli_close($con);
$statement = mysqli_prepare("SELECT * FROM user WHERE username = ? AND password = ? ");
并且mysqli_prepare
需要$con
参数(http://php.net/manual/fr/mysqli.prepare.php):
$statement = mysqli_prepare($con,"SELECT * FROM user WHERE username = ? AND password = ? ");