user1634845
user1634845

Reputation:

Making a PHP/MySQL Search Engine

In a site I'm making, I need a search engine to find songs for people to listen to. I have it working to the point that it can get info from the database and display them on the page. The problem comes when there are two songs with the same name. I have a system so the results will go to separate links, but when I search them they display the same image even though there is two separate sources for them. It also will make extra results for some reason. Here's my code:

<?php
if (isset($_GET['q'])) {
    $q = $_GET['q'];
    mysql_connect('********', '********', '********');
    mysql_select_db('********');
    $query = mysql_query("SELECT * FROM ******** WHERE title LIKE '$q'");
    $numrows = mysql_num_rows($query);
} else {
    echo "
<span style='font-family: Helvetica, Arial;font-weight: bold;font-size: 25px;'>Search</span>
<form action='http://www.example.com/search' method='get'>
<input placeholder='Search for music' type='text' name='q' style='font-weight:bold;padding:5px;width:300px;border-top-left-radius: 4px;border-top-right-radius: 10px;border-bottom-left-radius: 10px;border-bottom-right-radius: 4px;border: 3px solid gray;background-color:#000000;color:#FFFFFF;' />
</form>
    ";
}
if ($numrows != 0) {
    $index = 0;
    $results = array();
    while($row = mysql_fetch_assoc($query)) {
        $results[$index] = $row;
        $index++;
        foreach ($results as $result) {
            $url = "http://www.example.com?id=" . $row['id'];
            $title = $row['title'];
            $arturl = $row['art_url'];
            if ($_GET['q'] != "") {
                echo "
                    <a href='$url'>
                    <table>
                    <tr style='text-align:left;'>
                    <td><img src='$arturl' style='width:100px;height:100px;'></td>
                    <td>
                    <span class='songTitle'>$title</span>
                    <br/>
                    <span class='songArtist'>By: Unknown</span>
                    </td>
                    </tr>
                    </table>
                    </a>
                    <br />              
                ";
            }
        }   
    }
} else {
    if ($_GET['q'] != "") {
        echo "
<span style='font-family: Helvetica, Arial;font-weight: bold;font-size: 25px;'>Search</span>
<form action='********' method='get'>
<input placeholder='Search for music' type='text' name='q' style='font-weight:bold;padding:5px;width:300px;border-top-left-radius: 4px;border-top-right-radius: 10px;border-bottom-left-radius: 10px;border-bottom-right-radius: 4px;border: 3px solid gray;background-color:#000000;color:#FFFFFF;' />
</form>
        ";
        echo "<br />No results where found.";
    }
}
?>

Upvotes: 1

Views: 568

Answers (2)

Phphelp
Phphelp

Reputation: 1330

while($row = mysql_fetch_array($result))Try $result['title'] instead of $row['title'];. Same goes to $url and $arturl

This should work.

if ($numrows != 0) {
    $index = 0;
    $results = array();
    while($row = mysql_fetch_array($query)) {
            $url = "http://www.example.com?id=" . $row['id'];
            $title = $row['title'];
            $arturl = $row['art_url'];
            if ($_GET['q'] != "") {
                echo "
                    <a href='$url'>
                    <table>
                    <tr style='text-align:left;'>
                    <td><img src='$arturl' style='width:100px;height:100px;'></td>
                    <td>
                    <span class='songTitle'>$title</span>
                    <br/>
                    <span class='songArtist'>By: Unknown</span>
                    </td>
                    </tr>
                    </table>
                    </a>
                    <br />              
                ";
            }
    }           
    }

Upvotes: 1

Bizarre
Bizarre

Reputation: 116

your codes seem right. However, to clarify, try to echo $arturl and see if it is getting the right source name. And also check the value in the database. Last thing is try to check whether the source image is in the correct folder. Try and give the feedback.

Upvotes: 0

Related Questions