3raser Posted May 1, 2010 Share Posted May 1, 2010 (Like is the hardest MySQL syntax/code/whatever...XD) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>RuneScape Servers - Search</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div id="container"> <div id="header"><h1>RuneScape Servers - Search</h1></div> <div id="wrapper"> <div id="content"> <p><strong>Search</strong></p> <p><?php $connect = mysql_connect("$mhost","$muser","$mpassword") or die("Connection failed!"); mysql_select_db("$mdatabase") or die("Database fail!"); $search = $_GET['keywords']; $type = $_GET['category']; $search = mysql_real_escape_string($search); $type = mysql_real_escape_string($type); if (!$search || !$type) { echo "<b>(Note: enter in all information that has a * by it)</b><br>*Enter in some keywords: <form action='search.php' method='GET'><input type='text' name='keywords'><br><input type='radio' name='category' value='Non-project server' />*Non-project<br><input type='radio' name='category' value='Project server'>*Project<br><input type='submit'></form>"; } else { echo "Viewing results for <b>".$search ."</b> for a <b>". $type ."</b> server<br>"; //max displayed per page $per_page = 10; //get start variable $start = $_GET['start']; //count records $record_count = mysql_num_rows(mysql_query("SELECT * FROM tables")); //count max pages $max_pages = $record_count / $per_page; //may come out as decimal if (!$start) $start = 0; //display data $get = mysql_query("SELECT * FROM tables WHERE category='$type' ORDER BY likes DESC LIMIT $start, $per_page"); while ($row = mysql_fetch_assoc($get)) { echo "<br><b>". $row['servername'] ." (". $row['category'] .") created by ". $row['serverowner'] ." (". $row[views] ." views & ". $row[likes] ." likes!)</b><br>"; } //setup prev and next variables $prev = $start - $per_page; $next = $start + $per_page; //show prev button if (!($start<=0)) echo "<a href='index.php?start=$prev'>Prev</a> "; //show page numbers //set variable for first page $i=1; for ($x=0;$x<$record_count;$x=$x+$per_page) { if ($start!=$x) echo " <a href='index.php?start=$x'>$i</a> "; else echo " <a href='index.php?start=$x'><b>$i</b></a> "; $i++; } //show next button if (!($start>=$record_count-$per_page)) echo " <a href='index.php?start=$next'>Next</a>"; } ?></p> </div> </div> <div id="navigation"> <p><strong>Menu</strong></p> <ul> <li><a href="search.php">Search</a></li> <li><a href="add.php">Add a project</a></li> </ul> </div> <div id="extra"> <p><strong>Read me!</strong></p> <p>The search page only shows 10 results per page!</p> </div> <div id="footer"> <p>Happy searching</p> </div> </div> </body> </html> In my code, how would I make it so $search could be used as keywords to search a name of a server LIKE his inputed keywords? Quote Link to comment https://forums.phpfreaks.com/topic/200347-better-searching-using-like/ Share on other sites More sharing options...
trq Posted May 1, 2010 Share Posted May 1, 2010 In my code, how would I make it so $search could be used as keywords to search a name of a server Insert it into your query in the right place. A better question is why on earth are you querying the same database table twice in the same script? Quote Link to comment https://forums.phpfreaks.com/topic/200347-better-searching-using-like/#findComment-1051414 Share on other sites More sharing options...
3raser Posted May 1, 2010 Author Share Posted May 1, 2010 In my code, how would I make it so $search could be used as keywords to search a name of a server Insert it into your query in the right place. A better question is why on earth are you querying the same database table twice in the same script? What do you mean? They are both used for different things? And, what you said doesn't help me.......at all. :/ Quote Link to comment https://forums.phpfreaks.com/topic/200347-better-searching-using-like/#findComment-1051416 Share on other sites More sharing options...
mikesta707 Posted May 1, 2010 Share Posted May 1, 2010 LIKE in Mysql is an operator, much like =. so if you want to use it then... well... use it. just replace the = with Like, for example SELECT column FROM table WHERE somecolumn LIKE 'someValue' also, the bit about 2 queries to the same table, it would be a lot more efficient if instead of preforming 2 queries, just preform one, store the data in an array, and then use that array Quote Link to comment https://forums.phpfreaks.com/topic/200347-better-searching-using-like/#findComment-1051425 Share on other sites More sharing options...
3raser Posted May 1, 2010 Author Share Posted May 1, 2010 Here is my code: //display data $get = mysql_query("SELECT * FROM tables WHERE category='$type' AND servername LIKE '$search' ORDER BY likes DESC LIMIT $start, $per_page"); while ($row = mysql_fetch_assoc($get)) It works, but you have to type in the EXACT name of the server for it to show. :/ Quote Link to comment https://forums.phpfreaks.com/topic/200347-better-searching-using-like/#findComment-1051570 Share on other sites More sharing options...
Mchl Posted May 1, 2010 Share Posted May 1, 2010 And what would you like (pun not intended)? Quote Link to comment https://forums.phpfreaks.com/topic/200347-better-searching-using-like/#findComment-1051583 Share on other sites More sharing options...
3raser Posted May 1, 2010 Author Share Posted May 1, 2010 And what would you like (pun not intended)? I'm just trying to get it to search a servername LIKE their keywords put in. But it's making it to were you have to make it EXACT. Quote Link to comment https://forums.phpfreaks.com/topic/200347-better-searching-using-like/#findComment-1051593 Share on other sites More sharing options...
Mchl Posted May 1, 2010 Share Posted May 1, 2010 Can you give an example? Because I think LIKE can not do what you want it to do. Quote Link to comment https://forums.phpfreaks.com/topic/200347-better-searching-using-like/#findComment-1051599 Share on other sites More sharing options...
3raser Posted May 1, 2010 Author Share Posted May 1, 2010 Can you give an example? Because I think LIKE can not do what you want it to do. I did. I gave an example like 3-4 posts up. Quote Link to comment https://forums.phpfreaks.com/topic/200347-better-searching-using-like/#findComment-1051605 Share on other sites More sharing options...
mikesta707 Posted May 1, 2010 Share Posted May 1, 2010 try using wildcards with like servername LIKE '%$search%' which will match a name with $search in it. (like if you typed myserv, it would match myserver, but NOTserver.) but like mchl said, it sounds like you are looking for a more comprehensive string match, like regex Quote Link to comment https://forums.phpfreaks.com/topic/200347-better-searching-using-like/#findComment-1051612 Share on other sites More sharing options...
3raser Posted May 1, 2010 Author Share Posted May 1, 2010 try using wildcards with like servername LIKE '%$search%' which will match a name with $search in it. (like if you typed myserv, it would match myserver, but NOTserver.) but like mchl said, it sounds like you are looking for a more comprehensive string match, like regex THANK YOU, IT WORKED! WOOO-HOO! Thank you! And thank you everyone else. Quote Link to comment https://forums.phpfreaks.com/topic/200347-better-searching-using-like/#findComment-1051613 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.