Jump to content

What's wrong with my php code?


jasoncdenson

Recommended Posts

I have received an error when I run this code:

Parse error: syntax error, unexpected 'while' (T_WHILE) in C:\wamp\www\SearchEngine\search.php on line 50

Code:

<?php
//php code goes here
include 'connect.php'; // for database connection
$query = $_GET['q'] // query
?>
<html>
    <head>
        <title>
            Brandon's Search Engine
        </title>
        <style type="text/css">
            #search-result {
                font-size: 22;
                margin: 5px;
                padding: 2px;
            }
            #search-result:hover {
                border-color: red;
            }
        </style>
    </head>
    <body>
        <form method="GET" action="search.php">
            <table>
                <tr>
                    <td>
                        <h2>
                            Brandon's Search Engine
                        </h2>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="text" value="<?php echo $_GET['q']; ?>" name="q" size="80" name="q"/>
                        <input type="submit" value="Search" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <?php
                        //SQL query
                        $stmt = "SELECT * FROM web WHERE title LIKE '%$query%' OR link LIKE '%$query%'";
                        $result = mysql_query($stmt);
                        $number_of_result = mysql_num_rows($result);
                        if($number_of_result < 1)
                            echo "No result found. Please try with other keyword.";
                        else
                        (
                                //results found here and display them
                                while($row = mysql_fetch_assoc($result))
                                (
                                    $title = $row["title"];
                                    $link = $row["link"];
                                    echo "<div id='search-result'>";
                                    echo "<div id='title'" . $title . "</div>";
                                    echo "<br />";
                                    echo "<div id='link'" . $link . "</div>";
                                    echo "</div>";
                                )
                        )
                        ?>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/284245-whats-wrong-with-my-php-code/
Share on other sites

You need to use curly braces for code blocks { and }. Not parenthesesis ( and )

                        if($number_of_result < 1)
                            echo "No result found. Please try with other keyword.";
                        else
                        { // open curly brace
                                //results found here and display them
                                while($row = mysql_fetch_assoc($result))
                                {  // open curly brace
                                    $title = $row["title"];
                                    $link = $row["link"];
                                    echo "<div id='search-result'>";
                                    echo "<div id='title'" . $title . "</div>";
                                    echo "<br />";
                                    echo "<div id='link'" . $link . "</div>";
                                    echo "</div>";
                                }  // close curly brace
                        }  // close curly brace
                        ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.