Jump to content

[SOLVED] checking to see if name is like any others in DB


dennismonsewicz

Recommended Posts

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?

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.

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.

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>';
													}
												}	

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.