Explain this for me?

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
xEzMikex
Forum Commoner
Posts: 38
Joined: Sat Jan 21, 2006 10:18 pm
Location: Canada

Explain this for me?

Post by xEzMikex »

$query = "SELECT * FROM `map` WHERE `id` = $id";

can somone explain what this would do, I have the general idia. Just need to make sure.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The specific line would set a variable $query to the string (after $id is parsed).

When given to a database it would, provided the table and column references are legal and $id is a valid value, would request all the columns (in the table 'map') from each record where the field 'id' matched $id.
xEzMikex
Forum Commoner
Posts: 38
Joined: Sat Jan 21, 2006 10:18 pm
Location: Canada

Post by xEzMikex »

Allright lets say I added new data to the table could I go

mike.php?id=1

would it show the data?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that would depend on where that statement is and whether you have register_globals on or not.. (they should be off) .. In which case, you would need to create $id, either via using $_GET['id'] or some other (magical) way.
xEzMikex
Forum Commoner
Posts: 38
Joined: Sat Jan 21, 2006 10:18 pm
Location: Canada

Post by xEzMikex »

Ugh, so confusing jeze. thanks for the help ill post again later with more info.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

xEzMikex wrote:Allright lets say I added new data to the table could I go

mike.php?id=1

would it show the data?
If you did something like:

Code: Select all

if (isset($_GET['id']) ) {
    $id = intval($_GET['id']);  // this makes sure nothing nasty is being injected into you SQL
    if ($id > 0) {
        $query = "SELECT * FROM `map` WHERE `id` = $id";
        $result = some_database_query($query);
        // check if the query returned any rows
        // if so then fetch data
    } else {
        // set error message here for "not valid id"
    }
} else {
    // set error message here for "no id passed"
}
(#10850)
xEzMikex
Forum Commoner
Posts: 38
Joined: Sat Jan 21, 2006 10:18 pm
Location: Canada

Post by xEzMikex »

yes but would this put it on the template or just show the info?
logic-8
Forum Newbie
Posts: 5
Joined: Sun Jan 22, 2006 6:29 pm

Post by logic-8 »

Looks to me like this will just queue the database to the id specified in the address line... so it can do whatever you want it to do by adding your code into the second if statement right about where the "//if so fetch data" comments are.
Post Reply