# Search engine for my web page



## GSquadron (Jun 14, 2011)

Hi guys!
I created a simple search engine, but i am a beginner so don't know what is the problem with it.
I created the search, but the problem is, whatever i write on it.... it shows everything.
I know the LIKE action, but i dunno how to use it, can anyone help on here?
The code i have written is this:

```
$result = mysql_query("SELECT * FROM student LIKE '%id%'");
```

Also, if i delete the like function everything i type doesn't matter, it shows everything on the database


----------



## Disparia (Jun 14, 2011)

```
$result = mysql_query("SELECT * FROM studenti [B]WHERE [I]column[/I][/B] LIKE '%[I]something[/I]%'");
```

Could do something like this for a single box name search:

```
SELECT * FROM studenti WHERE CONCAT(firstname, ' ', surname) LIKE '%[I]partialname[/I]%'
```

From your example, seems like you'd like to search every field? This is of course possible. What information do you have in the students table?


----------



## GSquadron (Jun 14, 2011)

In the student table i have:
id, name, surname, school, age, city, address, e-mail, password
What is the meaning of CONCAT?????


----------



## Disparia (Jun 14, 2011)

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

Concat is short for concatenation, or gluing strings together.

In my example I did a LIKE against the full name (name + surname). This is _similar_ to:


```
SELECT * FROM studenti WHERE name LIKE '%string%' OR surname LIKE '%string%'
```

The difference is that the first example could return results from searches like "jill s" or "hobar d" where as the second one would need to be a partial of either the name or surname.

Searches can be designed dozens of different ways. Could simply concat all the fields you want and search against it:


```
SELECT * FROM studenti WHERE CONCAT_WS(' ', name, surname, school, CAST(age AS CHAR), city, address, e-mail) LIKE '%string%'
# I assumed that age may be an integer type
```

Lacks the versatility of a Google or Yahoo search, but it's a start. From one input box a user could search for "tirana" or "18" or "joe" or anything else and possibly get results. What they couldn't do is search for all of them at the same time. Not without more coding.


----------



## theJesus (Jun 14, 2011)

Jizzler said:


> Lacks the versatility of a Google or Yahoo search, but it's a start. From one input box a user could search for "tirana" or "18" or "joe" or anything else and possibly get results. What they couldn't do is search for all of them at the same time. Not without more coding.


I'm not a coder (only took a simple "programming logic and problem-solving" class), but wouldn't it be possible to set up some sort of "if" or "case" statement to allow for various types of searches based on input (like if it's in quotes or has boolean operators such as AND, OR, etc.).  Or maybe there is a built-in or just simpler way of handling boolean operators in queries?  Or another way would be to have it perform different types of searches based on some checkboxes and/or radio buttons.  Just some ideas.


----------



## GSquadron (Jun 14, 2011)

@Jizzler
Is it possible to search for Aleks and it shows my name?
Or the person must put in ALL the name to find it?


----------



## Disparia (Jun 14, 2011)

theJesus said:


> I'm not a coder (only took a simple "programming logic and problem-solving" class), but wouldn't it be possible to set up some sort of "if" or "case" statement to allow for various types of searches based on input (like if it's in quotes or has boolean operators such as AND, OR, etc.).  Or maybe there is a built-in or just simpler way of handling boolean operators in queries?  Or another way would be to have it perform different types of searches based on some checkboxes and/or radio buttons.  Just some ideas.



Yup. Things like city and school could be drop-down boxes to narrow down results. And with a little work to recognize keywords, quotes, etc, the script could build a query that more accurately returns results.

By coincidence I'm coding up a search page for the company I'm contracting at right now. It's one dirty $%&*$%* database and at this time it can't be redesigned. I'm already up to 250 lines just to build the queries and it's only about half-way done! Also have several mapping files so users can choose one value and the search will include all variations and make use of regex patterns where needed. Much of it could be avoided if the db was well-normalized.




Aleksander Dishnica said:


> @Jizzler
> Is it possible to search for Aleks and it shows my name?
> Or the person must put in ALL the name to find it?



Yes. Because of the "%", it would also work with "le", "ek", "al", "ale", "lek" and any single letter in your name.

Depending on how you want your search to operate, you could certainly have multiple fields.


----------



## GSquadron (Jun 14, 2011)

Ok. The big problem i always have is the "PARSE ERROR" 
Parse error: syntax error, unexpected T_STRING in C:\Program Files (x86)\EasyPHP-5.3.6.0\www\regjistrohu.php on line *2*
So this is ALL the code:

```
<?php
$connection mysql_connect("localhost","root","");
if(!$connection)
	{
	die('Connection could not be done: ' . mysql_error());
	}

mysql_select_db("geoinformatic", $connection);

mysql_query($connection);
$id = $_POST['id'];
$emri = $_POST['name'];
$mbiemri = $_POST['surname'];
$sql="INSERT INTO student (id, name, surname)
VALUES
(´$_POST[id]´,´$_POST[name]´,´$_POST[surname]´)";

if (!mysql_query($sql, $connection))
  {
  die('Error: ' . mysql_error());
  }
echo "New registration added";

$result = mysql_query("SELECT * FROM student");

while($row = mysql_fetch_assoc($result))
  {
  echo $row['id'] . ' ' . $row['name'] . ' ' . $row['surname'];
  }

mysql_close($connection);
?>
```
Where the HELL is the problem here?


----------



## Zyon (Jun 15, 2011)

Line 2

$connection *=* mysql_connect("localhost","root","");

mysql_connect function should be returning a connection which is then assigned to $connection, just need the = sign.


----------



## GSquadron (Jun 15, 2011)

There was an error with my local server.
Anyway, i fixed all the errors


----------

