The_Monster
The_Monster

Reputation: 510

Retrieve data from php

I have a small piece of code that makes a connection with a webpage. When I login Iretrieve the user_id from the database and put that in a textbox. Then I want to retrieve the Firstname and Lastname from the database but when I do that I get a <br/> from the webpage. I cannot find out why I get the <br/> instead of the firstname.

This is my php code to handle the request that I get from Java

$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];
}

And this is the code that I use to send the data to 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;
    }
}

Can someone explain to me why I get the <br/> instead of the first name?

Thanks in advance

Upvotes: 2

Views: 131

Answers (1)

user3734231
user3734231

Reputation: 36

It could be that in the line where you select the $voornaam: "SELECT$detailFROM$tableWHERE$row= '$value'"

You use 3 different quotes: U use ", ', Try to replace thewith ' and then I think it should work.

So replace "SELECT$detailFROM$tableWHERE$row= '$value'" with "SELECT '$detail' FROM '$table' WHERE '$row' = '$value'"

Upvotes: 1

Related Questions