简体   繁体   中英

Trying to put table around results from query?

I can't put a table around my results from my code and need some help as I've tried but it comes back with "Parse error: syntax error, unexpected '<' in C:\\xampp\\htdocs\\search_go.php on line 27". So could I get help with how to insert a table?

<?php

//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);

//check whether the name parsed is empty
if($searchTerm == "")
    {
    echo "Enter name you are searching for.";
    exit();
}

//database connection info
$host = "localhost"; //server
$db = "calendar"; //database name
$user = "root"; //dabases user name
$pwd = ""; //password

//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);

//MYSQL search statement
$query = "SELECT * FROM caltbl WHERE evtDate LIKE '%$searchTerm%'";

$results = mysqli_query($link, $query);

<table>
/* check whether there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
    $output = "";
    while($row = mysqli_fetch_array($results))
    {
        <tr>
        $output .= "date: " . $row['evtDate'] . "<br />";
        $output .= "Name: " . $row['patient'] . "<br />";
        $output .= "Course: " . $row['patientId'] . "<br />";

    }
    echo $output;
}
else
    echo "There was no matching record for the name " . $searchTerm;
?>

You can't just insert a HTML tag inside PHP code:

You can however just use an echo to send it out directly:

echo "<table>";

while($row = mysqli_fetch_array($results))
{
    $output = "<tr>";
    // <tr> This is the problem line.
    $output .= "<tr>";

    $output .= "<td>date: " . $row['evtDate'] . "<br /></td>";
    $output .= "<td>Name: " . $row['patient'] . "<br /></td>";
    $output .= "<td>Course: " . $row['patientId'] . "<br /></td>";
    $output .= "</tr>";
    echo $output;
}

Additionally you didn't close your <tr> . I added some extra snippets to make each field a TD in the table and then closed the row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM