i was thinkg some thingklike this. i have a database
image->image_id, image, image_keyword, image_name ...etc

index.php- search bar

<form class='form6' method='post' action='search.php'>   
    <input type='text' name='search' class='field2'/>                      //search bar field
    <input type='submit' name='search' class='button' value='Search' />    //submit button
</form>

search.php

<?php
include("include/header.php");

$i = 0;
$search_p = $_POST["search"];
$terms = explode(" ", $search_p); //every space new key word

$query = "SELECT * FROM search WHERE";        //get data from sumbit button  - NOT SURE IF THIS IS RIGHT
foreach($terms as $each)
{
    $i++;
    if($i == 1)
        $query .= "image_keyword LIKE '%each%'";
    else
        $query .= "OR keyword LIKE '%each%'";
}

    //connet to database
    $query_s = mysql_query("SELECT * FROM image");
    $row = mysql_num_rows($query_s);
    if($row > 0)
    {
        while($row = mysqlfetch_assoc($query_s))
        {
            $image_keyword_db = $rows['image_keyword'];
            $image_full_name_db = $rows['user_full_name'];
            $image_short_name_db = $rows['image_short_name'];
            $image_des_db = $rows['image_des'];

            echo "$image_short_name_db";
        }
    }
    else
    {
        echo "no result found";
    }
?>

problem is that it print every thing from database. i want to print if $search_p is equal to image_keywor, image_short_name, image_full_name...etc

Recommended Answers

All 6 Replies

On line 19 you select everything. You should execute $query there, the one you just built. One issue though, in your WHERE clause, %each% should be %$each%

Line 8 should be: SELECT * FROM image WHERE with a space at the end.

commented: ty +2

$query = "SELECT * FROM search WHERE ";

i am not sure what is "search".
is search from form submit button?
and if it is a button than what should i chose for *?

what is "OR keyword"? is that from sql database col name

You posted this. You should know what 'search' is. In this context it is a database table, that is why I suggested you change it to image.

<?php
include("include/header.php");

$i = 0;
$search_p = $_POST["search"];
$terms = explode(" ", $search_p); //every space new key word

$query = "SELECT * FROM image WHERE ";  //get data from sumbit button
foreach($terms as $each)
{
    $i++;
    if($i == 1)
        $query .= "image_keyword LIKE '%$each%' ";
    else
        $query .= "OR keyword LIKE '%$each%' ";
}

    //connet to database
    $query = mysql_query("SELECT * FROM image");
    $numrow = mysql_num_rows($query);
    if($numrow > 0)
    {
        while($row = mysql_fetch_assoc($query))
        {
            $image_keyword_db = $row['image_keyword'];
            $image_full_name_db = $row['image_full_name'];
            $image_short_name_db = $row['image_short_name'];
            $image_des_db = $row['image_des'];

            echo "$image_full_name_db<br/>";
        }
    }
    else
    {
        echo "no result found";
    }
?>

i made all the changes. and if i search let say "test"(which is a image_keyword col). than it print all the image_full_name

You did not change line 19, it should be:

$query = mysql_query($query);

thanks alot pritaeas, its working now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.