Jump to content


[php] variables from a url


6 replies to this topic

#1 skinnywhiteboy

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 25 November 2005 - 02:08 PM

Im making a tutorial script for my upcomming site and am having problems with url variables. Basicly i want to take the id number from the url, and then get that info from my database.
so if it was ?id=1 itll take id #1 from my database. Im already connected to the database, just need help on the url part of it

#2 Sicloan

    Young Padawan

  • Members
  • Pip
  • 31 posts

Posted 25 November 2005 - 03:20 PM

those extensions on the url are called Query Strings. Its easy to get:

//  link my look like this:
//  http://www.example.com/index.php?id=2

$var = $_GET['id'];

$query = "select * from TABLENAME where id = $var";

$result = mysql_query($query);

Edited by Sicloan, 25 November 2005 - 03:21 PM.


#3 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 25 November 2005 - 04:26 PM

That presents a security issue
<?php
$id = addslashes(str_replace('"', '&quote;', $_GET['id']));
$sql = "SELECT * FROM `table` WHERE `id` = $id";
$q = mysql_query($sql);
$r = mysql_fetch_assoc($q);
?>
To echo out the information use echo $r['field'];

#4 skinnywhiteboy

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 25 November 2005 - 04:54 PM

Thanks both, it works perfectly

#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 November 2005 - 05:20 PM

Info you can look at on the $_GET var: http://php.net/manual/en/reserved.variable...d.variables.get

As for Halopr0's code, there's a simpler built-in function that will work just as good, if not better (and will at least save space).
http://php.net/manual/en/function.mysql-re...cape-string.php

#6 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 25 November 2005 - 05:37 PM

Whoa, first time I have ever seen that function, thanks man :P

#7 Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 27 November 2005 - 08:12 AM

also remember to use intval() if you only need to get an id :ph34r:





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users