learning php

hi everyone… hoping for some help… I’m absolute newbie to php and mysql.
i’ve managed to set up a local environment and created a db with a table containing 5 fields:
id
name
address
city
state

i also created a simple form with 2 dropdown boxes for selection:

State
City

When i select a state and city on this form i want to get results containing names and addresses from my db for the selected state and city.

i’ve been trying to follow tutorials but totally confused about the php code. Is there a simple code to make this happen?

you need to make a query to the database calling only the records you wish to view,
something like this will work:
[php]<?php

$state = $_POST[‘state’]; // post data sent from form.
$city = $_POST[‘city’]; // post data sent from form.

// make a query.
$result = mysql_result("SELECT * FROM database WHERE state = ‘$state’ ORDER BY ‘$city’ ASC ");
?>[/php]

The query works like this:
use a form to set the variables $state and $city
fetch all records from the database where the state matches $state
and list them all in alphabetical order by the city.
(this is optional, you can order by any field you like and can be reversed ‘z’ first using DESC);

To access the data in the array do something like this:
[php]<?php
while($array = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $array[‘name’];
echo ‘
’;
echo $array[‘city’];
}

// outputs:
John Doe
New York
[/php]

I hope thats helps, if your still stumped let me know
:wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service