0

Could someone show me how I can do this or if it isn't possible then to show me another way?

Im trying to get the description that the user typed in and slice away some words and then get the first word and search if there is another word in my database just like it in the description of a word associated with that word

This whole thing is sort of like a dictionary

$word = (isset($_POST['word']) ? $_POST['word'] : null);
$description = (isset($_POST['description']) ? $_POST['description'] : 
null);
echo "<br>" . "<br>";//this is why its showing
$Slicer = array( ' a ', 'A ', 'The ', ' the ', ' an ', ' this ', ' that 
',
' these ', ' those ', ' my ', ' your ', ' his ', ' her ', ' its ',
' it\'s ', ' our ', ' their ', ' few ', ' little ', ' much ', ' many ',
' lot ', ' of ', ' most ', ' some ', ' any ', ' enough ', ' all ', ' 
both ',
' either ', ' neither ', ' each ', ' every ', ' other ', ' another ',
' Here ', ' I ',    ' me ', ' mine ',   ' myself ', ' i ',
' you ', ' your\'s ', ' yourself ', ' he ', ' him ', ' his ',
' himself ', ' she ', ' her\'s ', ' herself ', ' itself ',
' we ', ' us ', ' our ', ' ours ', ' ourselves ', ' yourselves ',
' they ',   ' them ', ' theirs ', ' themselves ', ' no ', ' none ',
' not ', ' any ', ' few ', ' few ', ' several ', ' great ', ' deal ',
' lot ', ' lots ', ' large ', ' amount ', ' plenty ', ' more ',
' fewer ', ' fewest ', ' less ', ' least ', ' what ', 'she\'s',
'the ', ' to ', ' for ', ' something ', ' or ', ' used ',
' represent ', ' in ', ' by ', ' are ', ' often ', ' called ', 'a ', 
'.');

$sliced = str_replace($Slicer,' ',$description);
echo $sliced;
echo "<br>";
$SWords = (explode(" ",$sliced));
echo "<br>";

$FirstWord = $SWords[1];
echo "<br>";
echo $FirstWord;
echo "<br>";
$test = "test";

$sql = "SELECT * FROM WordDatabase WHERE description LIKE '" 
.$FirstWord."'"; 

I have more code and Im connected to my database and everything

Jose Salgado
  • 29
  • 1
  • 6

1 Answers1

0

1 is not the first index of a numeric array in php. All indexes in php (and pretty much every other programming language) starts with 0.

So the first word will be at

$FirstWord = $SWords[0];

And you are most likely using LIKE wrong. Like without a wildcard character (%) will in most cases (but not allways) result in the same as =. So WHERE description LIKE 'foo' will probably do the same as WHERE description = 'foo' (only potentially slower), which is most likely not what you want.

Perhaps you want something like this:

SELECT * FROM WordDatabase WHERE description LIKE 'foo%'

or

SELECT * FROM WordDatabase WHERE description LIKE '%foo%'

Also, your code is likely susceptible for SQL injection attacks. Please don't concatenate user input directly to a query yourself. Instead, use mysqli or PDO or a framework utilizing one of these two and use their methods to create a prepared statement like this:

$mysqli = new mysqli($host, $user, $pass, $db);
if (mysqli_connect_errno()) {
  throw new Exception('Could not connect to database: ' . mysqli_connect_error());
}
if ($sql = $mysqli->prepare("SELECT description FROM WordDatabase WHERE description LIKE ?")) {
    $sql->bind_param("s", $FirstWord . '%');
    $sql->execute();
    $sql->bind_result($description);
    $sql->fetch();
    $sql->close();
} else {
    # ....
}
$mysqli->close();
Xyz
  • 5,955
  • 5
  • 40
  • 58