logicopinion Posted October 7, 2007 Share Posted October 7, 2007 hello, i want to search my database and i created search.php file. here is the code: <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("mynews") or die(mysql_error()); $query = "SELECT * FROM newsdb WHERE date like '%$keyword%' or title like '%$keyword%' or rte1 like '%$keyword%'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { print "<table cellpadding=0 cellspacing= width=100% width=700>"; print "<tr><td height=20 class=\"date\" bgcolor=#E0E0E0 style=\"BORDER-BOTTOM:1px #1E90FF solid;\"> <B>"; echo $row['date']; print "</B></td></tr>"; print "<tr><td>"; print "<br>"; echo $row['title']; print "</td></td>"; print "<tr><td align=right>"; echo "<a class=\"more\" href='read_news.php?id=".$row['id'] ."'>დაწვრილებით...</a>"; print "<br><br>"; print "</td></td>"; print "</table>"; } ?> but if the search field is empty and i press "search" button, search.php displays all the data from DB, instead of this i want something like this: if searchword is not specified to echo "please insert search word" but if the searchword is inserted to do the action from above code. how can do that? i know its if statmant function but could not write it right way.. please help me. Quote Link to comment https://forums.phpfreaks.com/topic/72176-searchphp-if-statmant/ Share on other sites More sharing options...
JasonLewis Posted October 7, 2007 Share Posted October 7, 2007 like so.... <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("mynews") or die(mysql_error()); if(str_replace(" ", "", $keyword) == ""){ //replace spaces with nothing, so if they enter a spaces echo "You did not enter a keyword, please go back."; //your echo }else{ $query = "SELECT * FROM newsdb WHERE date like '%$keyword%' or title like '%$keyword%' or rte1 like '%$keyword%'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { print "<table cellpadding=0 cellspacing= width=100% width=700>"; print "<tr><td height=20 class=\"date\" bgcolor=#E0E0E0 style=\"BORDER-BOTTOM:1px #1E90FF solid;\"> <B>"; echo $row['date']; print "</B></td></tr>"; print "<tr><td>"; print "<br>"; echo $row['title']; print "</td></td>"; print "<tr><td align=right>"; echo "<a class=\"more\" href='read_news.php?id=".$row['id'] ."'>დაწვრილებით...</a>"; print "<br><br>"; print "</td></td>"; print "</table>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72176-searchphp-if-statmant/#findComment-363944 Share on other sites More sharing options...
logicopinion Posted October 7, 2007 Author Share Posted October 7, 2007 thank you man, its something close to what i want, but now insert i word or no,, in both cases it tisplays warning message: you did not enter search word, go back Quote Link to comment https://forums.phpfreaks.com/topic/72176-searchphp-if-statmant/#findComment-363946 Share on other sites More sharing options...
logicopinion Posted October 7, 2007 Author Share Posted October 7, 2007 any ideas? ??? Quote Link to comment https://forums.phpfreaks.com/topic/72176-searchphp-if-statmant/#findComment-363953 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.