0

My Page url is

http://www.example.org/index.php?option=com_used&prid=100&id=200
http://www.example.org/index.php?option=com_used&prid=100&id=201
http://www.example.org/index.php?option=com_used&prid=101&id=202
http://www.example.org/index.php?option=com_used&prid=99&id=203

Here id is unique field name

I have below code to display mobile number of seller for field column where a used toy listing id is mentioned

$db = JFactory::getDbo();
$db->setQuery("SELECT `mobile` FROM `#__used_variants` WHERE `id`='200' LIMIT 1");
return $db->loadResult();

I am unsure how to capture in id field value from url and pass in php code, so that when page id is 200 then mobile number of seller having id as 200 is shown, and wen page is 201, seller having id as 201 is shown, same for 202 and 203 and so on

Can any one help in in

Ruchika
  • 503
  • 1
  • 8
  • 26

2 Answers2

1

You can get parameters in PHP using $_GET.

Try this:

  echo $_GET['id'];

It will return the id from url.

So you can try this:

 $id = $_GET['id'];

 $db = JFactory::getDbo();
 $db->setQuery("SELECT `mobile` FROM `#__used_variants` WHERE `id`='$id' LIMIT 1");
 return $db->loadResult();
Kinshuk Lahiri
  • 1,468
  • 10
  • 23
  • 1
    But - can it lead to sql injection as posted in by Jibin – Ruchika Oct 16 '16 at 09:33
  • 1
    @Ruchika Use prepared statements and parameterized queries. You can read here http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – Kinshuk Lahiri Oct 16 '16 at 10:12
1

The $_GET holds the parameters passed via url.

use $_GET['id'] to access the id parameter

$db->setQuery("SELECT `mobile` FROM `#__used_variants` WHERE `id`=".$_GET['id']." LIMIT 1");

Ensure that you escape the $_GET['id'] parameter to prevent SQL injection

Jibin Mathew
  • 4,816
  • 4
  • 40
  • 68
  • Hello, Thanks - you mean to say this is a better solution that the 1st one as posted in by Kinshuk. Also - does .$_GET['id'] prevent SQL Injection or how to escape the same. Pl advise. regds – Ruchika Oct 16 '16 at 09:30
  • 1
    @Ruchika check this out http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 You can prevent sql injection depending on the driver you have. – Jibin Mathew Oct 16 '16 at 17:15