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?

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.