krio Posted September 9, 2009 Share Posted September 9, 2009 I have a mysql database. It locates the data in the table correctly if the table matches, but if I search without inputting data it returns the entire table for some reason.. why is it returning the entire table? function searchDatabase($search) { $serialsearcher = substr ( $search, - 4 ); $querysearch = ("SELECT * FROM parts WHERE serialnumber LIKE '%$serialsearcher' OR name LIKE '%$search%'"); $resultsearch = @mysql_query ( $querysearch ); while ( $search = $this->fetch_array ( $resultsearch )) { echo "<tr>"; echo "<td><a href='#' title='Lorem Ipsum'>" . $search ['serialnumber'] . "</a></td>"; echo "<td><a href='#' title='Lorem Ipsum'>" . $search ['name'] . "</a></td>"; echo "<td><a href='#' title='Lorem Ipsum'>" . $search ['part'] . "</a></td>"; echo "<td class='last'><a href='content_partdatabase.php?delete=" . $search ['serialnumber'] . "'>Delete</a></td>"; echo "</tr>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/173725-solved-database-returning-all-variables-if-when-it-cant-fine-where/ Share on other sites More sharing options...
TeNDoLLA Posted September 9, 2009 Share Posted September 9, 2009 You posted a function, that does nothing at all alone unless you call that function which is not present in the code you posted. So where are you calling this function? And that function does not even return anything. Quote Link to comment https://forums.phpfreaks.com/topic/173725-solved-database-returning-all-variables-if-when-it-cant-fine-where/#findComment-915748 Share on other sites More sharing options...
krio Posted September 9, 2009 Author Share Posted September 9, 2009 I'm just sending in a variable $search that contains string. The problem is that if $search is empty, the entire table is being shown Quote Link to comment https://forums.phpfreaks.com/topic/173725-solved-database-returning-all-variables-if-when-it-cant-fine-where/#findComment-915751 Share on other sites More sharing options...
PrinceOfDragons Posted September 9, 2009 Share Posted September 9, 2009 Add in somewhere that if its blank dont submit the search. Quote Link to comment https://forums.phpfreaks.com/topic/173725-solved-database-returning-all-variables-if-when-it-cant-fine-where/#findComment-915754 Share on other sites More sharing options...
krio Posted September 9, 2009 Author Share Posted September 9, 2009 yea I did that, but I'm just trying to get to the bottom of why it's happening when I don't think it should be thanks! Quote Link to comment https://forums.phpfreaks.com/topic/173725-solved-database-returning-all-variables-if-when-it-cant-fine-where/#findComment-915757 Share on other sites More sharing options...
MadTechie Posted September 9, 2009 Share Posted September 9, 2009 Well the function does echo's data! the reason your getting all the records is because if $serialsearcher is empty then you are querying the following:~ serialnumber LIKE '%' which means any! add $serialsearcher to the function parameters and try something like this (rough draft) $q =""; if(!empty($serialsearcher)) $q .= "serialnumber LIKE '%$serialsearcher'"; if(!empty($search)) { if(!empty($q)) $q .= " OR "; $q .= "name LIKE '%$search%'"; } $querysearch = ("SELECT * FROM parts WHERE $q "); Quote Link to comment https://forums.phpfreaks.com/topic/173725-solved-database-returning-all-variables-if-when-it-cant-fine-where/#findComment-915758 Share on other sites More sharing options...
krio Posted September 9, 2009 Author Share Posted September 9, 2009 perfect! thanks! I didn't realize that queries worked that way.. I just thought it would fail Quote Link to comment https://forums.phpfreaks.com/topic/173725-solved-database-returning-all-variables-if-when-it-cant-fine-where/#findComment-915761 Share on other sites More sharing options...
MadTechie Posted September 10, 2009 Share Posted September 10, 2009 if you searched WHERE usernames LIKE '%madtechie%' OR subjects LIKE '%' it finds every subject as subjects LIKE '%' means any subject so the username part is kinda pointless I'll also like to point out you shouldn't have the @ on the query as it omit errors (which during development you want to see) if you update to $resultsearch = mysql_query ( $querysearch ) or die($querysearch.mysql_error()); your get a better error report for debugging Quote Link to comment https://forums.phpfreaks.com/topic/173725-solved-database-returning-all-variables-if-when-it-cant-fine-where/#findComment-915764 Share on other sites More sharing options...
krio Posted September 10, 2009 Author Share Posted September 10, 2009 thanks again... These have been confusing me for some time now ! =D Quote Link to comment https://forums.phpfreaks.com/topic/173725-solved-database-returning-all-variables-if-when-it-cant-fine-where/#findComment-915851 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.