PDA

View Full Version : Select query help


Liz_W
05-18-2005, 12:53 AM
I am trying to write a query that takes input from a web form and selects the applicable information from a database and then outputs the results in html.

The database is called car, there are five columns, in this order;
--------------------------------------------------------------------------
registration

make

model

colour

ratecode
--------------------------------------------------------------------------

What I want to do is display the colour and registration of the cars that match the selected make and model from a drop down list.

I tried a go at it but ended up in a mess as you see, can someone help me please.


if (!($dbh = DBI->connect("DBI:mysql:databasename:localhost", 'username', 'password')))
{
&outputErr("couldn't connect to database".DBI->errstr);
}


if (!($query = $dbh->prepare("SELECT * FROM car Where 'make' And 'model' =='$type'")))
{
&outputErr("couldn't prepare statement".$dbh->errstr);
}

#execute the query
if (!($query->execute()))
{
&outputErr("couldn't execute statement".$query->errstr);
}
while (@car = $query->fetchrow_array())
{
$key=$car[0];
$value=$car[1];

}

$query->finish;

Kiwi
05-18-2005, 01:48 AM
SELECT * FROM car
WHERE 'make' = '$make'
AND 'model' = '$model'

You will need to write come code to convert your $type variable to two variables: $make and $model.

In my idealised world of normalised databases, these would in fact be well behaved foreign keys with some sort of referential integrity enforced (eg - a table of 'MAKES' containing the possible car makes; then a table of 'MODELS' that would reference 'MAKES' and show the models from the car maker. And your CAR table would be restricted to valid MAKE & MODEL combinations - although since each model could only be associated with one make, even this would contain redundant data).

Liz_W
05-18-2005, 03:23 AM
That helped a little. I appreciate it :)