TheEddy Posted August 4, 2010 Share Posted August 4, 2010 $sql = "SELECT * FROM `users` WHERE `$searchField` like '%$search%'"; I still have to put in the EXACT value, it can't be similar. Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/ Share on other sites More sharing options...
Pikachu2000 Posted August 4, 2010 Share Posted August 4, 2010 Post an example of the search term you used and the data in the database that you feel it should have returned. Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1094911 Share on other sites More sharing options...
TheEddy Posted August 4, 2010 Author Share Posted August 4, 2010 Post an example of the search term you used and the data in the database that you feel it should have returned. $searchField would have been "emailAddress" and $search should have been "webmaster" and should have pulled: [email protected] and [email protected] Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1095185 Share on other sites More sharing options...
Pikachu2000 Posted August 4, 2010 Share Posted August 4, 2010 That looks like it would work as written. Have you echoed the query string to make sure it contains the values it should contain? Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1095204 Share on other sites More sharing options...
TheEddy Posted August 4, 2010 Author Share Posted August 4, 2010 That looks like it would work as written. Have you echoed the query string to make sure it contains the values it should contain? This is what I have for results: $sql = "SELECT * FROM `users` WHERE `$searchField` LIKE '%$search%'"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result); $userName = $row['userName']; $uid = $row['userID']; if (mysql_num_rows($result) == 1) { print (" <ul> "); while ($row = mysql_num_rows($result)){ print (" <li><a href=\"memberprofiles.php?uid=$uid\" title\"$userName\">$userName</a></li> "); exit; } print (" </ul> "); } else { print (" There were no results based on your criteria. "); } Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1095220 Share on other sites More sharing options...
Pikachu2000 Posted August 4, 2010 Share Posted August 4, 2010 This is your problem: if (mysql_num_rows($result) == 1) {. If more than one result is returned, it lists none of them and echos the "no results" message. If you expect exactly one record to match, you should not be using a WHERE . . . LIKE query. If you expect zero or more records to match, you shouldn't be restricting the result set to one record, and you should echo the results in a while() loop. Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1095224 Share on other sites More sharing options...
marcus Posted August 4, 2010 Share Posted August 4, 2010 Pikachu is absolutely correct. The function mysql_num_rows will return the amount of entries selected from your query, it doesn't return a boolean. Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1095229 Share on other sites More sharing options...
TheEddy Posted August 4, 2010 Author Share Posted August 4, 2010 if (mysql_num_rows($result) != 0) { print (" <ul> "); while ($row = mysql_fetch_assoc($result)){ print (" <li><a href=\"memberprofiles.php?uid=$uid\" title\"$userName\">$userName</a></li> "); exit; } print (" </ul> "); } else { print (" There were no results based on your criteria. "); } I don't know how I made that mistake where I had that equal to 1. But I don't know how to loop the results Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1095243 Share on other sites More sharing options...
marcus Posted August 4, 2010 Share Posted August 4, 2010 mysql_fetch_assoc stores the values in an array. So you could either use the list function like: while(list($uid,$userName) = mysql_fetch_assoc($result)){ Or when you print the HTML you would do something like echo "<li><a href=\"membersprofiles.php?uid=".$row['uid']."\" title=\"".$row['userName']."\">".$row['userName']."</a></li>\n"; Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1095251 Share on other sites More sharing options...
TheEddy Posted August 4, 2010 Author Share Posted August 4, 2010 What does "\n" do? Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1095320 Share on other sites More sharing options...
marcus Posted August 4, 2010 Share Posted August 4, 2010 It just creates a new line in your output, makes your HTML looks neater in the end. Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1095322 Share on other sites More sharing options...
TheEddy Posted August 4, 2010 Author Share Posted August 4, 2010 mysql_fetch_assoc stores the values in an array. So you could either use the list function like: while(list($uid,$userName) = mysql_fetch_assoc($result)){ Or when you print the HTML you would do something like echo "<li><a href=\"membersprofiles.php?uid=".$row['uid']."\" title=\"".$row['userName']."\">".$row['userName']."</a></li>\n"; That doesn't work btw. Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1095326 Share on other sites More sharing options...
marcus Posted August 4, 2010 Share Posted August 4, 2010 What are you getting for your output? Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1095328 Share on other sites More sharing options...
TheEddy Posted August 4, 2010 Author Share Posted August 4, 2010 What are you getting for your output? Nothing will appear unless I search for email and only one result when there should be two. If I search for email, I don't get the message that there is nothing there but no results are shown. Link to comment https://forums.phpfreaks.com/topic/209737-problem-with-like-in-sql-query/#findComment-1095335 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.