I've written exactly one PHP program thus far - search against a text file using "preg_match". It looked something like this (and it worked):
<pre>
<?php
$string = $_POST['keywords'];
$matches = 0;
$file = fopen("archive.txt", "r") or die("Cannot open file!\n");
while ($line = fgets($file, 1024)) {
if (preg_match("/$string/", $line)) {
echo $line;
echo "\n";
$matches = 1;
}
}
if ($matches == 0) {
echo "sorry, I haven’t written a haiku about ".$string." yet.";
}
fclose($file);
?>
</pre>
Now I need to match a string in a MySQL table and return the row as a match, and I'm struggling with the syntax. Here's what I've come up with so far. It produces a blank page.
<pre>
<?php
$string = $_POST['keywords'];
// Connect to Database
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("archive") or die(mysql_error());
$data = mysql_query("SELECT * FROM archive_text WHERE text LIKE $string")
or die(mysql_error());
if ($data = "") {
echo "sorry, I haven’t written anything about ".$string." yet.";
}
echo $data;
echo "\n";
}
?>
</pre>
In both cases, the string is passed from a simple HTML search page to the PHP program via a post.
Any pointers would be appreciated.