Extracting specific data from database?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcc_shane
Forum Newbie
Posts: 22
Joined: Sat May 12, 2012 1:47 pm

Extracting specific data from database?

Post by mcc_shane »

Hi Everyone,

How do I extract specific data from a database without having to write hundreds of different mysql commands?

For example, if I want to extract then display only "car dealers" in New York City on my "New York City Page" --->> mydomain.com/new-york-city/used-cars. How would I make a script that will only "pull" or "extract" new york city car dealers without having to write different mysql commands?

Thanks
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Extracting specific data from database?

Post by Grizzzzzzzzzz »

use $_SERVER to obtain the current url (it'll either be PHP_SELF or REQUEST_URI, depending on how your pages are created)

parse that, and strip out the bit you want, and store it in a variable, then use that variable in your sql

Code: Select all

$place_name = explode("/", $_SERVER['PHP_SELF']);
$sql = "SELECT * FROM table WHERE city = '$place_name[1]'";
considering the example of mydomain.com/new-york-city/used-cars, you'll probably want to futher format the string, i.e:

Code: Select all

$place_name = explode("/", $_SERVER['PHP_SELF']);
$formatted_place_name = str_replace("-", " ", $place_name[1]);
$sql = "SELECT * FROM table WHERE city = '$formatted_place_name'";
to ensure you're querying for 'new york city', not 'new-york-city' (depends how you've stored the name)

only additional things would be validation & any additional statements you want to query for
Post Reply