Forum Moderators: coopster

Message Too Old, No Replies

Problem with displaying results...

         

drunkenmonkey79

10:29 pm on Dec 4, 2004 (gmt 0)

10+ Year Member



Hey Everybody! Newbie here just starting to get my feet wet with PHP.

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]

Salsa

12:49 am on Dec 5, 2004 (gmt 0)

10+ Year Member



Welcome to Webmaster World, drunkenmonkey.

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]

henry0

1:07 pm on Dec 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It will be better to get in the habit of:
<<<
echo "Here are the results:<br><br>";
echo "<table width=90% align=center border=1><tr>
<td align=center bgcolor=#00FFFF>Event</td>
>>>
doing the following instead of the above
<<<
echo ("Here are the results:<br><br>
<table width=90% align=center border=1><tr>
<td align=center bgcolor=#00FFFF>Event</td>");
At least it helps you verifying where starts and end the "echo".
and then don't forget escaping:
\"#000000\"
but you have done it further below.

good luck

drunkenmonkey79

6:17 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



Salsa! Thanks for that advice about the single quotes. I put those in and it works now! I really appreciate it, you rock!

Henry0, thanks for the syntax advice, I will def try to keep that in mind.

THIS SITE ROCKS!

Salsa

6:38 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



I'm glad the quote thing worked for you. And, of course, ignore my "two elses" advice. Because this board removes indenting, it makes it harder to read posted code, and on a second look I can see that you used your elses properly.