0

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); 
} 
?>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
bbpgrs
  • 33
  • 1
  • 4
  • 4
    You need to use a server to run PHP, it doesn't run when you access it as a local file. – Barmar Apr 01 '16 at 02:20
  • What's exactly being printed? – Indrasis Datta Apr 01 '16 at 03:20
  • Barmar is right in that you need a server to run PHP code. A web browser can only deal with Javascript, HTML, and CSS, and any derivatives basically. The Server runs the PHP code, and uses its output to create a new webpage that is exclusively of those 3 for the browser. Without it, the PHP code is treated as HTML (print("$field
    "), 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

0 Answers0