Page 1 of 1

PHP MySQL string LIKE

Posted: Mon Jul 26, 2010 6:32 am
by atsa1
Hi.
I have this little problem. I take data from MySQL using search field.
I have this code to compare if any field "number" has the same value and try to echo it on the screen.

Code: Select all

$query = mysql_query("SELECT id, date, kood, kogus, nimetus, number, kes, komm, staatus, ladu FROM tellimus WHERE kood like '%$otsi%' or nimetus like '%$otsi%' or number like '%$otsi%' or komm like '%$otsi%' order by id");
//display row
while ($row = mysql_fetch_array($query)) {
$numberotsi=$row['number'];
}
    $_SESSION['vali'] = "WHERE kood like '%$otsi%' or nimetus like '%$otsi%' or komm like '%$otsi%' or number like '%$otsi%' or number like '%$numberotsi%' order by id" or die ('Error: ' . mysql_error());
Problem is: It understand what it supposed to too. Like: When i search a code (find data from field "kood") it understands that i have to look for the "number" field, compare if any other MySQL line has the same value and print them both not only the line that has the "Code" i serached. That's because i want to know if that person has ordered anything else or only this certain piece. Well it works for me but only if i have exactly the same value in the "number" field.

When i have a code lets say:
1234 and the orderers name is John Smith 987654
2345 and the orderers name is Smith John 987654

Now i search the product code 1234 i get the result: (search engine uses the string and looks fields: kood, nimetus,number)
1234 and the name is John Smith.
Well i know that he has some more products. Now when i serch the Smith (again search engine uses the string and looks fields: kood, nimetus,number) i get the result:
1234 and the orderers name is John Smith 987654
2345 and the orderers name is Smith John 987654
bacuse string Smith is in number filed and the other line has the Smith string in it it displays both lines.
But when i search: John Smith 987654
it wont show me the line: Smith John 987654

How can i do that: John Smith 987654 = Smith John 987654
That's the main question.

Re: PHP MySQL string LIKE

Posted: Mon Jul 26, 2010 7:52 am
by atsa1
Never mind. Got it working useing substr();

Code: Select all

else if ($_GET['cmd']=='otsime')
{
     $otsi = $_POST['otsi'];
mysql_connect ("localhost", "root", "radikas") or die ('Error: ' . mysql_error());
mysql_select_db ("foral");
$query = mysql_query("SELECT id, date, kood, kogus, nimetus, number, kes, komm, staatus, ladu FROM tellimus WHERE kood like '%$otsi%' or nimetus like '%$otsi%' or number like '%$otsi%' or komm like '%$otsi%' order by id");
//display row
while ($row = mysql_fetch_array($query)) {
$numberotsi=$row['number'];
$rest = substr("$numberotsi", 0, 6);
}
    $_SESSION['vali'] = "WHERE kood like '%$otsi%' or nimetus like '%$otsi%' or komm like '%$otsi%' or number like '%$otsi%' or number like '%$rest%' order by id" or die ('Error: ' . mysql_error());
}
problem was i used "number like %$numberotsi%" what took use the whole line in the column. All i needed is to make it shorter so it will use only 6 first letters from the string input. So substr("$numberotsi", 0, 6); will use letters from 1-6 from the cell. so if the word was: january now it will use only januar and search complete and working like i want.

And again: Hopeing for someone else to help you is stupid. Better is to find solutions on your own.