Jump to content

Php and Mysql error.


supesham

Recommended Posts

Hey everyone,

 

So i was following a tutorial on youtube, it is a simple search and display program using mysql and php. i have completed 4 of 5 tutorials and when i enter a keyword in the textfield search and subit, it displays all the results in my database instead of the one. here is my code, any help would be very helpful thanks.

 

$button = $_GET['submit'];
$search = $_GET['search'];


if(strlen($search)<2)
echo "Search term too short";
else
{
echo "You searched for <b>$search</b><hr size='1'>";
mysql_connect("localhost", "root", "supersham");
mysql_select_db("carbreaker");


$keywords = $getrow['keywords'];

//explode our search term
$search_exploded = explode(" ", $search);

$search_each = 0;
while ($search_each < count($search_exploded)) 
{ 
if ($search_each == 0) 
{ 
$construct = "keywords LIKE '%" . $search_each . "%'"; 
} 
else 
{ 
$construct .= " AND keywords LIKE '%" . $search_each . "%'"; 
} 
$search_each++; 
}

//echo out construct
$construct = "SELECT * FROM stock WHERE $construct";
$run = mysql_query($construct);

$foundnum = mysql_num_rows($run);

if($foundnum==0)
echo "No Stock Found";
else{
echo "$foundnum results found<br>";
while($runrows = mysql_fetch_assoc($run))
{
$make = $runrows['make'];
$model = $runrows['model'];
$year = $runrows['year'];
$cc= $runrows['cc'];
$fuel = $runrows['fuel'];
$doors = $runrows['doors'];
$body = $runrows['body'];
$date = $runrows['date'];

echo "<b>$make</b> $model $year $cc $fuel $doors $body $date<br>";

}
}
}


?>

Link to comment
Share on other sites

Wow, that's some pretty rough code. I can't even follow what you are doing in some places. For example:

$keywords = $getrow['keywords'];

 

I don't see $getrow defined anywhere.

 

Anyway, I think the problem is the consrtuction of your WHERE clause. You could have validated this easily enough by simply echoing the query to the page. It looks as if you are using the counter as the seach term not the actual search terms. Plus, there is a problem in the logic if you were to have two consecutive spaces.

 

But, you are making it much more difficuly than it needs to be with all those loops.

 

$button = trim($_GET['submit']);
$search_phrase = trim($_GET['search']);

if(strlen($search_phrase)<2)
{
    echo "Search term too short";
}
else
{
    echo "You searched for <b>$search_phrase</b><hr size=\"1\">\n";
    mysql_connect("localhost", "root", "supersham");
    mysql_select_db("carbreaker");

    $keywords = $getrow['keywords'];

    //explode our search term
    $search_words = explode(" ", $search_phrase);

    $like_parts = array();
    foreach($search_words as $word) 
    {
        if($word<>'')
        {
            $like_parts[] = "keywords LIKE '%" . mysql_real_escape_string($word} . "%'";
        }
    }
    $where_clause = (count($like_parts)>0) ? "WHERE ".implode(' OR ', $like_parts) : '';

    //echo out construct
    $construct = "SELECT * FROM stock $where_clause";
    $run = mysql_query($construct);

    $foundnum = mysql_num_rows($run);
    if($foundnum==0)
    {
        echo "No Stock Found";
    }
    else
    {
        echo "$foundnum results found<br />\n";
        while($row = mysql_fetch_assoc($run))
        {
            echo "<b>{$row['make']}</b> {$row['model']} {$row['year']} {$row['cc']} {$row['fuel']} {$row['doors']} {$row['body']} {$row['date']}<br />\n";
        }
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.