Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl accessing a database

         

aline

5:46 pm on May 3, 2003 (gmt 0)

10+ Year Member



I'm trying to set up a search page whereby a user can enter a word and Perl would trawl through the database to come up with any records attached to this word. The simplified version of the code looks like this:

#!c:/perl/bin/perl.exe
use DBI;
$,="\t";
use CGI ":standard";
$search=param("search");

print"content-type:text/html\n\n";
print"<html>\n";
print"<body bgcolor=\"fffbec\">";
print"<center><b><font face=\"Comic Sans Ms\"><font size=\"6\"><font color=\"990000\"> Search results</b></font face></font size></font color></center><br><br>";
print"<b>You asked for <font color=\"990000\">$search</font color>. Here is the result:</b><br><br>";

$db=DBI->connect("dbi:mysql:Web");
$equery="select eventname,host,venue,standingmin,price from EVENT WHERE eventname='$search'¦¦host='$search' ¦¦venue='$search'¦¦standingmin='$search'¦¦ price='$search'";
$e=$db->prepare($equery);
$e->execute();

print"<br><br><br><br><ul><li><b>Event details</b></li></ul><table border=\"1\" bgcolor=\"yellow\" width=\"60%\"><tr><td align=\"center\"><b>Name</b></td><td align=\"center\"><b>Host</b></td><td align=\"center\"><b> Venue</td><td align=\"center\"><b>Standinmin</b></td><td align=\"center\"> <b>Price</b></td><td align=\"center\"></td></tr>";
while(@eresult=$e->fetchrow())
{print"<tr>";
for($index=0;$index<@eresult;$index++)
{
print "<td align=\"center\">$eresult[$index]</td>";

}
print"</tr>";
}

print"</table>";

Everything works fine until I added ¦¦$price='$search' where suddenly records that do not correspond at all with the word entered are displayed. Could anyone explain why this can be the case?
Thank you

jatar_k

6:30 pm on May 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



i assume price is a numeric field? could just be getting confused trying to compare numeric and char, not really sure.

aline

7:06 pm on May 3, 2003 (gmt 0)

10+ Year Member



I thought about that but standinmin is also a numeric but get the right records. :-(

jatar_k

7:10 pm on May 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I dont really know, seems strange, what are the column types for those 2 cols? are they the same?

float and int or some other difference?

aline

7:32 pm on May 3, 2003 (gmt 0)

10+ Year Member



They are the same, both INT.

Storyteller

6:07 am on May 4, 2003 (gmt 0)

10+ Year Member



First off, I'd enable DBI tracing by adding $db->trace(1) and then check Apache error log to see what's actually going on on the lower level. Need to know whether the problem in DB- or code-related.

aline

3:21 pm on May 4, 2003 (gmt 0)

10+ Year Member



Here is what Apache is saying:

[Sun May 04 16:08:43 2003] [error] [client 127.0.0.1] <- prepare('select eventname,host,venue,standingmin,price from EVENT WHERE eventname='spiky'¦¦host='spiky'¦¦venue='spiky'¦¦standingmin='spiky'¦¦price=spiky' CODE)= DBI::st=HASH(0x369fdc) at search.pl line 59., referer: [localhost...]

I think the problem comes when the price in the database is 0. Any other price behaves properly and are only displayed when they correspond to the word entered whereas the prices that are 0 are always displayed even when they are totally unrelated to the word entered. Is there a special way of dealing with these null values?

BCMG_Scott

12:52 pm on May 5, 2003 (gmt 0)

10+ Year Member



Based on your error message it looks like you may be missing a single quote before "spiky":

eventname='spiky'¦¦host='spiky'¦¦venue='spiky'¦¦standingmin='spiky'¦¦price=spiky' CODE)=

(see price=spiky')

However, your original post show the quote being there?

Scott

aline

2:53 pm on May 10, 2003 (gmt 0)

10+ Year Member



Yes the code does show the single quote but for some reason it seems to be missed out when the programme is run. I really don't understand what is happening!

Storyteller

8:29 pm on May 10, 2003 (gmt 0)

10+ Year Member



It starts to looks like a DBI bug. I'd try replacing prepare/execute with something like selectall_arrayref (there're many different flavors, see DBI manpage).