dennismonsewicz Posted September 18, 2008 Share Posted September 18, 2008 Ok so i have this script: $exists_query = mysql_num_rows(mysql_query("SELECT project FROM added_projects WHERE project LIKE %" . $project_name . "%'")); if($project_name == $exists_query) { while($results = mysql_fetch_object($exists_query)) { echo '<p>Here is a list of other projects that have similar Project Names:</p>'; echo $results->project; } } The code is supposed to loop through the db and find like project names and then display the results. But its not happening Any ideas? Link to comment https://forums.phpfreaks.com/topic/124856-solved-checking-to-see-if-name-is-like-any-others-in-db/ Share on other sites More sharing options...
ratcateme Posted September 18, 2008 Share Posted September 18, 2008 try this $result = mysql_query("SELECT project FROM added_projects WHERE project LIKE %" . $project_name . "%'"); $rows = mysql_num_rows($result); if ($rows != 0) { while ($result = mysql_fetch_object($result)) { echo '<p>Here is a list of other projects that have similar Project Names:</p>'; echo $results->project; } } Scott. Link to comment https://forums.phpfreaks.com/topic/124856-solved-checking-to-see-if-name-is-like-any-others-in-db/#findComment-645041 Share on other sites More sharing options...
akitchin Posted September 18, 2008 Share Posted September 18, 2008 that won't fix the real reason this isn't working, which is a syntax error: "SELECT project FROM added_projects WHERE project LIKE %" . $project_name . "%'" is missing a single quote before the first % symbol. this would be clear if you added a die() clause to your query to capture any syntax errors. Link to comment https://forums.phpfreaks.com/topic/124856-solved-checking-to-see-if-name-is-like-any-others-in-db/#findComment-645043 Share on other sites More sharing options...
dennismonsewicz Posted September 18, 2008 Author Share Posted September 18, 2008 ok I have fixed the syntax error, but the results are only displaying one result when there are 4 results in the tested DB that are exactly the same. Any ideas? Updated code: $result = mysql_query("SELECT * FROM added_projects WHERE project LIKE '%" . $project_name . "%'") or die(mysql_error()); $rows = mysql_num_rows($result); if ($rows > 0) { while ($result = mysql_fetch_object($result)) { echo '<p>Here is a list of other projects that have similar Project Names:</p>'; echo '<p><a href="tools.php?action=view&id=' . $result->id . '">' . $result->project . '</a></p>'; } } Link to comment https://forums.phpfreaks.com/topic/124856-solved-checking-to-see-if-name-is-like-any-others-in-db/#findComment-645050 Share on other sites More sharing options...
akitchin Posted September 18, 2008 Share Posted September 18, 2008 while ($result = mysql_fetch_object($result)) you're overwriting the resource with the first row, so when it tries again, mysql_fetch_object() isn't being passed a resource. change the first $result to $row or whatever you fancy that isn't $result. Link to comment https://forums.phpfreaks.com/topic/124856-solved-checking-to-see-if-name-is-like-any-others-in-db/#findComment-645051 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.