Page 1 of 1

Problems making a search page

Posted: Mon Aug 29, 2005 12:54 pm
by Extremest
I am having all kinds of problems making a search page for my md5 db. What I need is when a user puts in the md5 hash it will then take the first 2 characters from the hash and that is the table name. Then make the search string the hash minus the first 2 characters. Then search for the hash in the db with the table that it got from the hash to find a match then display the full hash and the plaintext form of it from the table.

I created the tables like this hash VARCHAR(30) BINARY NOT NULL, text TEXT, PRIMARY KEY(hash) TYPE=MYISAM

Every page I have found or tried to make will not do anything it seems. Can someone plz help me out.

Posted: Mon Aug 29, 2005 1:08 pm
by feyd
I don't see a point in having multiple tables of the same structure like this.. :?

Posted: Mon Aug 29, 2005 1:13 pm
by Extremest
reason for this is md5 hash has only 256 start sets. Meaning the first 2 characters of the hash. So right now I have 256 tables and around 45000 rows per table. For a total just over 11 million. to have one table of that would be hard. so bunch of tables.

Posted: Mon Aug 29, 2005 1:17 pm
by feyd
would be hard? MySQL handles large tables quite well. Plus, the way you have it, will use a lot more server space, fyi.

Posted: Mon Aug 29, 2005 1:23 pm
by Extremest
well it is going to end up having over 100 million entries. This is the search page I made but it does not work. Maybe someone can find the problem in it or something

Code: Select all

<html>
<head>
	<title>MD5 Hash Results</title>
</head>
<body>
<h1>MD5 Hash Results</h1>
<?
	trim($searchterm);
	$table=substr($searchterm,0,2);
	$searchterm=substr($searchterm,2,30);
	$searchterm=addslashes($searchterm);
$db=mysql_connect("host", "user", "pass");

if (!db)
{
	echo "Error: Could not connect to database.";
	exit;
}

mysql_select_db("md5");
$query = "select * from md5 where ".$table." like '%".$searchterm."%'";
$result = mysql_query($query);
$num_results=mysql_num_rows($result);

echo "<p>Number of hashes found: ".$num_results."</p>";

for ($i=0; $i <$num_results; $i++)
{
	$row=mysql_fetch_array($result);
	echo "<p>".($i+1).". Hash: ";
	echo htmlspecialchars( stripslashes($row["hash"]));
	echo htmlspecialchars( stripslashes($row["text"]));
	echo "</p>;
}

?>

</body>
</html>
I know there is stuff wrong in there. I have made 3 completely different search pages and none of them seem to work.

Posted: Mon Aug 29, 2005 1:33 pm
by feyd
your query is trying to select against a table named md5 (which is a MySQL function, you may need to use backticks on it) looking for a column named $table, which, by your table structure would not exist.

Posted: Mon Aug 29, 2005 1:44 pm
by Extremest
ok I changed it to this

Code: Select all

$query = "select * from ".$table." where hash like '%".$searchterm."%'";
$table should be the first 2 characters of the hash. although this still don't work and for some reason I am getting an $end unexpected end of file in my error log for apache.

Also I am not sure if it is grabbing the value from the form. Here is the code for the form.

Code: Select all

<html>
<head>
	<title>MD5 Hash Lookup</title>
</head>

<body>
	<h1>MD5 Hash Lookup</h1>

	<form action="results.php" method="post">
	<br>
	Enter MD5 Hash:<br>
	<input name="searchterm" type=text>
	<br>
	<input type=submit value="search">
	</form>
</body>
</html>
I am sure I am just messed up here somewhere.

Posted: Mon Aug 29, 2005 2:51 pm
by Extremest
I just happened to notice that my db is set for latin1 and not for utf8 like the standard db that is in mysql. Can that make a difference when searching. I noticed that phpmyadmin converted the string from utf8 to latin1.

Posted: Mon Aug 29, 2005 4:58 pm
by feyd
if you echo $query, what does it show?

Posted: Mon Aug 29, 2005 6:29 pm
by Extremest
ok I have it now. This is my new search page and it works perfect.

Code: Select all

<?php

$mysql = mysql_connect("localhost","root","password");
$database = "MD5";

if($_GET["search"] == "on") {

	$hash = strtolower($_POST["hash"]);

	if(strlen($hash) == "32") {

		for($i=0; $i<strlen($hash); $i++) {
			$character = substr($hash,$i,1);
			for($ii=0; $ii<16; $ii++) {
				if(dechex($ii) == $character)
				{
					$checkscore[$i] = "pass";
				}

			}

		}

		if(count($checkscore) != strlen($hash)) {
			$_result = "Not a valid MD5 hash."; $hash = "";
		} else {

			$q = "SELECT text FROM `".substr($hash,0,2)."` WHERE
hash='".substr($hash,2,30)."' LIMIT 1";
			$result = mysql_db_query($database,$q,$mysql) or die(mysql_error());
			$row = mysql_fetch_array($result);
			$_result = $row["text"];
		}

	} else {
		$_result = "Not a valid MD5 hash."; $hash = "";
	}

}
?>

			<span>
				Search the Database
				<br /><br />
		 	</span>

			<form action="?category=01-2&search=on" method="post">
			<input  name="hash" class="text" type="text" size="60"
				maxlength="32" title="Paste MD5 hash here, then click search." />
		  	<input  class="submit" type="submit" value="Search" title="Click to
search." />
			</form>

			<span class="no-border normal-font1-10pt">
<?
		echo $hash." - ".$_result;

?>
		 	</span>
Thanks guys.