i cannot search my PHP array values into my MySQL Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
criseltungala
Forum Newbie
Posts: 1
Joined: Mon Jul 12, 2010 8:37 pm

i cannot search my PHP array values into my MySQL Database

Post 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);
?>
	
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

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

Post 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);
Post Reply