Forum Moderators: coopster
I am trying to preform a keyword search on my database, and display the matching results.
I only have two entries in the database, so if you search for "Prince" it should return one of those columns.
So here is where I am running into the problem. Once I hit submit, it looks like $result is not being assigned a value. And it outputs that I have problems.
Here is my code:
<?
if ($search) // perform search only if a string was entered.
{
mysql_connect('host','userid','password') or die ("Problem connecting to Database");
mysql_select_db(db114469701) or die( "Unable to select database");
$query = "select * from tickets WHERE event like `%$search%`";
$result = mysql_db_query("db114469701", $query);
if ($result)
{
echo "Here are the results:<br><br>";
echo "<table width=90% align=center border=1><tr>
<td align=center bgcolor=#00FFFF>Event</td>
<td align=center bgcolor=#00FFFF>Seat</td>
<td align=center bgcolor=#00FFFF>Cost</td>
<td align=center bgcolor=#00FFFF>Quantity</td>
</tr>";
while ($r = mysql_fetch_array($result)) { // Begin while
$ts = $r["TimeStamp"];
$event = $r["Event"];
echo "<tr>
<td>$ts</td>
<td>$event</td>
</tr>
<tr> <td colspan=4 bgcolor=\"#ffffa0\">$comment</td>
</tr>";
} // end while
echo "</table>";
} else { echo "problems...."; }
} else {
echo "Search string is empty. <br> Go back and type a string to search";
}
include ('links.x');
?>
One question I had was that for the following line:
$result = mysql_db_query("db114469701", $query);
"db114469701" is the name of my database, should this be the name of my table instead?
I have not really worked with the code within the IF statement since $result was not being populated. Could it be a problem within the IF statement?
Please let me know if I am missing any information that would help to clearify my questions. I was on a roll till I hit this speed bump, any input would be appreciated.
Thanks everyone!
[edited by: coopster at 11:44 pm (utc) on Dec. 4, 2004]
[edit reason] removed urls per TOS [webmasterworld.com] [/edit]
Regarding your question about mysql_db_query(), it looks like you've used it correctly, but since you've already called mysql_select_db(), why not just call your query with mysql_query(), like:
$result = mysql_query($query);
Then test $result with:
if (!$result) die("select * from tickets... failed: ".mysql_error()); Notice the call to mysql_error() in the call to die(). By including that, if the query fails, MySQL will give you some hints as to why. I'd also add it to the die calls after your other MySQL functions.
At a glance, I didn't see any glaring errors in your code. But I do question quoting `%$search%` in backticks. I'd just use single quotes. (...I just tested quoting with `, and it broke my code.)
It looks like you're off to a good start. Try the mysql_error() thing and change those quotes, and see if that helps.
I wish you well.
[edited] Oh yes, watch your if/elseif/else statements. You have an 'if' followed by two 'else' statements. I think you'll need to put your second else, "else {echo "Search string is empty...)," in a separate if statement, with its own conditions--or start out with that, like,
[pre]if(/* test to see if string is empty */);
elseif($result) {...}
else { echo "problems...."; }[pre]
[/edited]
good luck