Page 1 of 1

i cannot search my PHP array values into my MySQL Database

Posted: Mon Jul 12, 2010 9:15 pm
by criseltungala
Hi. I'm having some issues with displaying the content of my database if I'm searching it with an array value on my PHP code. I am trying to create a code wherein the user will input 1 or multiple imperative sentences and then the program will divide it with the first set of delimiters and then after that, it will be subdivided again with the second set. After each word has been tokenized, I want to search for the word in my database and give its equivalent. For example:

if my input string is "Hello! How are you? I'm fine thank you.", the results will be:
  • Array [0] -> Hello! Array [1] -> How are you? I'm fine thank you
for Hello
  • Array [0] -> Hello
for How are you? I'm fine thank you.
  • Array [0] -> How Array [1] -> are Array [2] -> you Array [3] -> I Array [4] -> m Array [5] -> fine Array [6] -> thank Array [7] -> you
What i need to do is to compare each tokenized words into my database and give its equivalent word category like for the word "are" it will display "verb". I'm just new to php and I'm kinda lost already. Here's the copy of the code:

Code: Select all

<?php
$con = mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("phrasal_lexicon_database", $con);
if ($_POST['submit'])
{		
	$wordname_form = $_POST['wordname'];
	$delimiter="/[.!:;]/";
	echo "Input string is <b>$wordname_form</b>. <br/>";
	$token=preg_split($delimiter, "$wordname_form", -1, PREG_SPLIT_NO_EMPTY);
	echo "<br/> The splitted words are: <br />";
	print_r($token);
	$counter =count($token);
	echo "<br/>The input string is divided into <b> $counter</b>.<br/>";

	if (is_array($token))
	{
		foreach ($token as $o)
		{
			$delimiter2="/[ ,.\/<>?;\\':\"\[\]\{\}\t\n\r~`!@#$%^&*()-=\\\_+|]/";
			$token2=preg_split($delimiter2, $o, -1, PREG_SPLIT_NO_EMPTY);
			echo "<br/> The tokenized words from <b> $o </b> are: <br />";
			print_r($token2);	

			for($i = 0; $i < count($token); $i++)
 			{	
				foreach($token2 as $key=>$value)
				{
					$extract=mysql_query("select * from word_categories where id= '. $token2'");
					$numrows=mysql_num_rows($extract);
					while ($row=mysql_fetch_assoc($extract));
					{
						$wordnames=$row['word_name'];
						$wordcategory=$row['word_category'];
						echo "$wordcategory";
					}
 				}
			}
		}
	}
	else 
	{
		echo"Failed";
	}
	
}
mysql_close($con);
?>
	

Re: i cannot search my PHP array values into my MySQL Databa

Posted: Tue Jul 13, 2010 9:19 am
by Jade
Why not just replace the spaces in the word with wildcards? You would need to strip punctuation and append wildcards to the beginning and end of the string so if they enter something like "Hi! How are you doing today?" you can pass it to mysql as:

Code: Select all

$result = mysql_query("SELECT * FROM database WHERE fieldname LIKE '%Hi%How%are%you%doing%today%'")
or die (mysql_error());

$row = mysql_fetch_array($result);
print_r($row);