0

Im trying to do a search function at the moment. I have done it before, but now I have to take the values from a Form in one PHP File and use it in another PHP File.

So I try to save the query in a global variable, but I keep getting errors.

"$GLOBALS['searchquery'] = SELECT Titel, Vorname, Nachname, Unternehmen, Gruppe FROM mitglieder WHERE Titel LIKE '%$GLOBALS['searchq']%' OR Vorname LIKE '%$GLOBALS['searchq']%' OR Nachname LIKE '%$GLOBALS['searchq']%' OR Unternehmen LIKE '%$GLOBALS['searchq']%' OR Gruppe LIKE '%$GLOBALS['searchq']%'";

If I do it like this it says: Unexpected "

$GLOBALS['searchquery'] = "SELECT Titel, Vorname, Nachname, Unternehmen, Gruppe FROM mitglieder WHERE Titel LIKE '".%$GLOBALS['searchq']%."' OR Vorname LIKE '".%$GLOBALS['searchq']%."' OR Nachname LIKE '".%$GLOBALS['searchq']%."' OR Unternehmen LIKE '".%$GLOBALS['searchq']%."' OR Gruppe LIKE '".%$GLOBALS['searchq']%."' ";

If I do it like this it says: Unexpected %

$GLOBALS['searchquery'] = "SELECT Titel, Vorname, Nachname, Unternehmen, Gruppe FROM mitglieder WHERE Titel LIKE '"%.$GLOBALS['searchq'].%"' OR Vorname LIKE '"%.$GLOBALS['searchq'].%"' OR Nachname LIKE '"%.$GLOBALS['searchq'].%"' OR Unternehmen LIKE '"%.$GLOBALS['searchq'].%"' OR Gruppe LIKE '"%.$GLOBALS['searchq'].%"' ";

And If I do it like this it says: Unexpected .

I realize theres alot of similar questions, but I havent found one that uses % so maybe that changes something about how I have to do the query?

w00ds98
  • 141
  • 8
  • try `'%".$GLOBALS['searchq']."%'` – Rahul Aug 31 '18 at 06:57
  • Not sure why you are using `$GLOBALS` for this in the first place? – Nigel Ren Aug 31 '18 at 07:18
  • @NigelRen When I outsourced my dbConnection to a seperate php file I only could use the connect variable when it was defined as a global one. So I assumed that whenever I want to use a variable in another php file it needs to be global? Is there a better alternative? – w00ds98 Aug 31 '18 at 07:33
  • Normally if you want to give access to variables or data in a function, you would pass it as a parameter. With classes, people tend to have started to use Dependency Injection (DI). https://stackoverflow.com/questions/12445972/stop-using-global-in-php and https://stackoverflow.com/questions/18562752/understanding-ioc-containers-and-dependency-injection may be worth a read. – Nigel Ren Aug 31 '18 at 07:38
  • Thanks! Always happy to improve my code! – w00ds98 Aug 31 '18 at 07:43

1 Answers1

0

you are putting % in a wrong place. It should be:

$GLOBALS['searchquery'] = "SELECT Titel, Vorname, Nachname, Unternehmen, Gruppe FROM mitglieder WHERE Titel LIKE '%".$GLOBALS['searchq']."%' OR Vorname LIKE '%".$GLOBALS['searchq']."%' OR Nachname LIKE '%".$GLOBALS['searchq']."%' OR Unternehmen LIKE '%".$GLOBALS['searchq']."%' OR Gruppe LIKE '%".$GLOBALS['searchq']."%'";
Tunker
  • 117
  • 9