I am very, very new to HTML/PHP so I'm probably going to misuse terminology, bear with me please.
I'm trying to print the results of my SQL query in HTML in a <p>
element using <br>
to separate them. I know it's not the best way but it's an easy one and will help me practice this.
Anyway my problem is instead of printing the "movieName" attribute I'm trying to print it dumps some of the PHP code into the <p>
element body instead.
I don't know what information to provide to make it easier on you to spot the problem so please request any relevant details.
Here's the php code (I've "censored" my info because you can easily access my student account with it):
<?php
if (array_key_exists('searchInput', $_GET)) {
$search = $_GET['searchInput'];
// Connecting, selecting database
$dbconn = pg_connect("host=**** port=15432 dbname=**** user=**** password=****")
or die('Could not connect: ' . pg_last_error());
// Performing SQL query
$query = "SELECT movieName
FROM DatabaseProject.Movie M
WHERE M.movieName LIKE $search";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
// Printing results in HTML
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$field = $row["movieName"];
print("$field<br>");
}
} else {
print("0 results");
};
// Free resultset
pg_free_result($result);
// Closing connection
pg_close($dbconn);
}
?>
This is what I get printed into the webpage:
num_rows > 0) { // output data of each row
while($row = $result->fetch_assoc()) {
$field = $row["movieName"];
print("$field
");
}
} else {
print("0 results");
};
// Free resultset pg_free_result($result);
// Closing connection pg_close($dbconn);
}
?>
"), the
moves the :) to the next line in output like it should in html). Your best bet is to learn how to turn your computer into a PHP server with a PHP development Eviroment. I personally use WAMP, but there are many other options that may be better for you. – Ryan Apr 07 '16 at 21:46