Re: creating the php search script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Again Gab

Wrote my earlier response in the early hours whilst a bit tired so only spotted the first error in the query.

No offense but there's actually so many errors I won't waste your or my time by listing them, apart from this one:

You run the query as $numresults=mysql_query($query2);

but the query is declared as $query = "select * from table where etc.

This never works - ever! Trust me:-)


Anyway - this is what I came up with:

$name = trim($_GET['q']);

$query = "SELECT * 
	    FROM student 
	   WHERE name LIKE '$name%'
	ORDER BY name
	 ";

On a table containing 

select * from student;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | mark      |
|  2 | jo        |
|  3 | steve     |
|  4 | John      |
|  5 | dave      |
|  6 | johnathon |
|  7 | janet     |
|  8 | joanne    |
+----+-----------+

my query produces 

select * from student where name like 'jo%' order by name;
+----+-----------+
| id | name      |
+----+-----------+
|  2 | jo        |
|  8 | joanne    |
|  4 | John      |
|  6 | johnathon |
+----+-----------+

select * from student where name like 'john%' order by name;
+----+-----------+
| id | name      |
+----+-----------+
|  4 | John      |
|  6 | johnathon |
+----+-----------+

Really couldn't work out what you were trying to do with LIMIT - $s will always be empty the 1st time its tested so will always be allocated a value of 0, which once again will result in no results being returned. Also if you want to append it to the original $query use the concatenation operator .=

Hope this has been helpful, but you really need to get hold of a good book on MySQL - I started with, & still use as a handy reference, the MySQL 5.0 Certification Study Guide published by MySQL AS. If you're a diligent student you could even become a MySQL 5.0 Certified Developer

Good luck with the rest of your script

Mark


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux