Page 1 of 1

Explain this for me?

Posted: Sun Jan 22, 2006 10:46 pm
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.

Posted: Sun Jan 22, 2006 10:53 pm
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.

Posted: Sun Jan 22, 2006 10:55 pm
by xEzMikex
Allright lets say I added new data to the table could I go

mike.php?id=1

would it show the data?

Posted: Sun Jan 22, 2006 10:57 pm
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.

Posted: Sun Jan 22, 2006 10:59 pm
by xEzMikex
Ugh, so confusing jeze. thanks for the help ill post again later with more info.

Posted: Sun Jan 22, 2006 11:15 pm
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"
}

Posted: Sun Jan 22, 2006 11:44 pm
by xEzMikex
yes but would this put it on the template or just show the info?

Posted: Mon Jan 23, 2006 7:25 am
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.