I want to search image from mysql

Firstly, I want to tell you that I use blob. therefore, I upload some photos, and I would like to create a search bar so that I can name it tagfrom my database. My business right now is the following:

I can not find 2 words or more.

My database has a field called tagand kategori.

In tagI put data likezat ; proses ; obat ;

Separated ;(with a comma).

So here is my code for one search:

<html>
<body>
<?php
$conn = mysql_connect("localhost", "denis", "denis");

mysql_select_db("company");
echo($_POST["tag"]);
?>
<form method="POST" >
<font color='black'>Cari gambar:</font>
<input type="text" name="tag" value=" ">
<input type="submit" name="search" value="Cari"/>
</form>
    <?php   
    if(isset ($_POST["tag"]))
    {
    $sql2="select imageId from output_images where bukuId=". $_GET["id"]." and tag like '%".$_POST["tag"]."%' Order by imageId asc";


    }
    else
    {
    $sql2="select imageId from output_images where bukuId=". $_GET["id"]." Order by imageId asc limit 1";

    }`enter code here`
    $res2=mysql_query($sql2,$conn);
    while($row2=mysql_fetch_array($res2))
    {
        echo("<img src='http://"  . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/profile.php?image_id=". $row2["imageId"]. "' width='15%' height='30%'/>"); 

    }


    mysql_close();
    //echo("ada");
mysql_close($conn);

?>
</body>
</html>

How can I do this for 2 words or more?

I heard about the explosion, but I don’t understand how to use it.

this is my database using a tag to search for my image

+4
source share
4

mysql:

WHERE interests REGEXP 'tag1|tag2|tag3'
+1

, MySQL:

            $tags = "tag1 tag2 tag2";
            $Id = 1;
            $atags = explode(" ",$tags);
            $mytags = "";
            foreach($atags as $tag)
            {
            $mytags .= " tag like '%". $tag . "%' or";  
            }
            $sql2="select imageId from output_images where bukuId=". $Id." and ".$mytags."der by imageId asc";
            echo $sql2;
0

Good that I understand. You need a search bar where you can enter image tags. The output of / $ _ POST ['tag'] will be similar to this tag1; tag2

You can use gaps and project each tag.

$data = explode(';', $_POST['tag']);
$i = 0;
foreach($data as $tags){
$i++;
  if($i == 1){
 $query .= 'AND tag like %'.$tags.'%';
}else{
$query .= 'OR tag like %'.$tags.'%';
}
}

$sql2="select imageId from output_images where bukuId=". $_GET["id"]." ".$query." Order by imageId asc";

this will give you sql query like this

SELECT imageId from output_images where bukuId= '5' AND tag='tag1' OR tag='tag2' Order by  imageId asc

EDIT

<html>
<body>
<?php
$conn = mysql_connect("localhost", "denis", "denis");

mysql_select_db("company");
echo($_POST["tag"]);
?>
<form method="POST" >
<font color='black'>Cari gambar:</font>
<input type="text" name="tag" value=" ">
<input type="submit" name="search" value="Cari"/>
</form>
    <?php   
    if(isset ($_POST["tag"]))
    {
    $data = explode(';', $_POST['tag']);
$i = 0;
foreach($data as $tags){
$i++;
  if($i == 1){
 $query .= 'AND tag like %'.$tags.'%';
}else{
$query .= 'OR tag like %'.$tags.'%';
}
}

$sql2="select imageId from output_images where bukuId=". $_GET["id"]." ".$query." Order by imageId asc";


    }
    else
    {
    $sql2="select imageId from output_images where bukuId=". $_GET["id"]." Order by imageId asc limit 1";

    }
    $res2=mysql_query($sql2,$conn);
    while($row2=mysql_fetch_array($res2))
    {
        echo("<img src='http://"  . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/profile.php?image_id=". $row2["imageId"]. "' width='15%' height='30%'/>"); 

    }


    mysql_close();
    //echo("ada");
mysql_close($conn);

?>
</body>
</html>

here is your code. You will see the changes I made.

0
source
        if(isset ($_POST["tag"]))
        {
        $tags = $_POST["tag"];
        $Id = $_GET["id"];
        $atags = explode(" ",$tags);
        $mytags = "";
        foreach($atags as $tag)
        {
        $mytags .= " tag like '%". $tag . "%' or";  
        }
        $sql2="select imageId from output_images where bukuId=". $Id." and tag like '%".$tags."%' Order by imageId asc";
        }

Here is your code changed

0
source

Source: https://habr.com/ru/post/1613193/


All Articles