gpt4 book ai didi

Java Php 连接错误从数据库中检索信息

转载 作者:太空宇宙 更新时间:2023-11-03 12:01:15 24 4
gpt4 key购买 nike

我刚开始使用 PHP 和 Java。我正在制作一个 Android 应用程序,但出现 SQL 语法错误...错误:

返回到 Java:

check the manual that corresponds to your MySQL server version for the right syntax to use near '@mail.com' at line 1. Any idea how I have to fix that.

我认为是 php 脚本的问题。我怎样才能解决这个问题。非常感谢任何帮助

        // Login by email and password if access success setId()
// Saved Email as static string "staticEmail" and used to get CustomerID from customer table
// Get & set CustomerID to "string qr_id" if email=".$email
/* error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near '@mail.com' at line 1
*/



<?php
//connect to MySQL database
mysql_connect("localhost","name","pass") or die(mysql_error());
mysql_select_db("tls_db");

$output = array();

if (isset($_GET['email'])){
$email = $_GET['email'];
$sql = mysql_query("select CustomerID from customer where email=".$email) or die(mysql_error());

while($row=mysql_fetch_assoc($sql)){
$output[] = $row;
}
mysql_close();
print(json_encode($output));
}
?>

Java:

        private void setId() {
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://"+URL+"/tls_db/log.php?email=" + staticEmail); //Post email 123@mail.com
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}

// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");

String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
is.close();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}

try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
// Get CustomerId and set to (static string qr_id)
qr_id = json_data.getString("CustomerID");
}
} catch (JSONException e1) {
//iv.setVisibility(View.GONE);
Toast.makeText(getBaseContext(), "Server Data Error",
Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
// Open class QRcode
Intent iSuccess = new Intent(Login.this, QRcode.class);
startActivity(iSuccess);
}

最佳答案

您需要在 SQL 查询中引用您的 $email:

 $sql = mysql_query("select CustomerID from customer where email='".$email."'") or die(mysql_error());

顺便说一句,您的代码容易受到 SQL 注入(inject)攻击。请务必阅读如何防范这种攻击媒介。

关于Java Php 连接错误从数据库中检索信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29105518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com