TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   [SOS]a search for a database ,double click here (http://www.talkphp.com/absolute-beginners/1881-sos-search-database-double-click-here.html)

webtuto 01-06-2008 09:37 PM

[SOS]a search for a database ,double click here
 
hi ,
well im trying to do a search form for my database here is the code =>
PHP Code:

<?php

if(!empty($_POST['search']) ) {
$con mysql_connect("localhost","root","") ;
$db mysql_select_db("test") ;
$sql "select * from webt where name='%$_POST[search]%' ";
$res mysql_query($sql) ;
if (
mysql_num_rows($res) == ) {
          echo 
"No result";
 }else{ while (
$row mysql_fetch_array($res) ) {
       
      echo 
$row[name]."<br><br>" 
       }
       
}
}else{ echo
"full the form"; } 

?>
<form method="post" action="search.php" >
<b>Search</b><input type="text" name="search" >
<input type="submit" value="Search" >
</form>

and in the database there is name='aa'
name is the name of the column
anyway when i delete "%" the code works great but when i add them it douesnt work *!*

bluesaga 01-06-2008 09:46 PM

% is a wildcard, that should only be used for LIKE mysql searches...

PHP Code:

$db mysql_select_db("test") ;
$sql "select * from webt where name='%$_POST[search]%' ";
$res mysql_query($sql) ; 

Should probably be:
PHP Code:

$db mysql_select_db("test") ;
$sql "select * from webt where name LIKE '%$_POST[search]%' ";
$res mysql_query($sql) ; 

please note, your script is FAR from secure and you should look at some topics here regarding:
http://www.talkphp.com/tips-tricks/1...rotection.html

Wildhoney 01-06-2008 09:47 PM

The actual syntax for that is like so:

sql Code:
SELECT
    myColumn
FROM
    myTable
WHERE
    myColumn
LIKE
    '%findThis%'

Edit: Meh young William, meh. Nothing else to add.

webtuto 01-06-2008 10:07 PM

thanks it works but for exemple you search "aaa" , i want it to give me the result from database as "aa"
but it douesnt :o
it give "no result"

EyeDentify 01-12-2008 01:15 PM

@webtuto

the % procent means that it replaces "anything" so for example you want to find a name ending with "Harrison" and you would search for "%Harrison". See the % in the beginning ?

Switch place of the % if you want to search for the name beginning with "Harrsion" as so "Harrison%". Catching on now ?

And if you want to search for a name containing "Harrison" then it would be "%Harrison%".

I belive you can use a ? questionmark for replacing a single char.
Correct me if im out on a limb here ?

And see Wildhoney and bluesaga´s post above.

I had trouble with the LIKE thing in my early days of MySQL queries to.

Good Luck.

/EyeDentify


All times are GMT. The time now is 09:46 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0