【问题标题】:Retrieve data from php从 php 中检索数据
【发布时间】:2014-06-12 10:29:04
【问题描述】:

我有一小段代码可以与网页建立连接。当我登录时,从数据库中检索 user_id 并将其放入文本框中。 然后我想从数据库中检索名字和姓氏,但是当我这样做时,我会从网页中得到一个<br/>。我不知道为什么我得到<br/> 而不是名字。

这是我处理从 Java 获得的请求的 php 代码

$user_id = $_SESSION['user'];
$voornaam = $con->select('voornaam', 'users', 'id', $user_id);
$achternaam = $con->select('achternaam', 'users', 'id', $user_id);

echo $voornaam;

public function select($detail, $table, $row, $value) {
    $query = mysqli_query($this->connect, "SELECT `$detail` FROM `$table` WHERE `$row` = '$value'");
    $associate = mysqli_fetch_assoc($query);
    return $associate[$detail];
}

这是我用来将数据发送到 php 的代码

public static String Select() {
    String mysql_type = "2"; // 2 = Select

    try {
        String urlParameters = "mysql_type=" + mysql_type;
        URL url = new URL("http://localhost:8080/HTTP_Connection/index.php");
        URLConnection conn = url.openConnection();

        conn.setDoOutput(true);

        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

        writer.write(urlParameters);
        writer.flush();

        String line;
        BufferedReader reader = new BufferedReader(new         InputStreamReader(conn.getInputStream()));

        while((line = reader.readLine())!= null)
        {
            return line;
        }

        writer.close();
        reader.close();

        return reader.readLine();

    } catch (IOException iox) {
        iox.printStackTrace();
        return null;
    }
}

谁能向我解释为什么我得到<br/>而不是名字?

提前致谢

【问题讨论】:

  • 注意:您的 public function select() 函数返回数组,而不是变量。所以你不能这样做,echo $voornaam;
  • @Ranjith 那么我该如何解决这个问题呢?
  • echo $voornaam; 替换为print_r($voornaam);。用您的问题更新结果。
  • @Ranjith 我仍然得到
    而不是我的 $voornaam
  • 让我们追踪你的 bug 卡在哪里?它可能在函数之外,也可能在函数内部。使用echoprint_r()

标签: java php mysql


【解决方案1】:

可能是在您选择 $voornaam 的行中: "SELECT$detailFROM$tableWHERE$row= '$value'"

您使用了 3 个不同的引号: 你使用 ", ', Try to replace thewith ' 然后我认为它应该可以工作。

所以替换 "SELECT$detailFROM$tableWHERE$row= '$value'" "SELECT '$detail' FROM '$table' WHERE '$row' = '$value'"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 2012-03-04
    • 2012-12-21
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多