-1

What would be the best or right way to get the value from the url using $_GET['id'] and to use it in a mysqli query?

Currently i'm using a regular expresion :

$id = preg_replace('/{([a-zA-Z0-9]+)}/', '', $_GET['id']);

but I don't know if this is the right or best way to do this.

Please explain your answer.

Zenel Shabani
  • 95
  • 1
  • 1
  • 10

2 Answers2

2

If you are already using mysqli then you don't need to take the pains of sanitization in your hands for basic stuff. You can use prepared statements which will take care of those

if (isset($_GET["id"])) {
    $id = $_GET['id'];
    $stmt = $mysqli->prepare("SELECT abc FROM table WHERE id=?");
    $stmt->bind_param("i", $id);   // or s if its a string
    $stmt->execute();
}

You can review examples here to find out how to deal with the result:

get_result() in PHP Manual

Dharman
  • 30,962
  • 25
  • 85
  • 135
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
0

You are reinventing the wheel. Depending on how you access db you may do not need do much (i.e. PDO will deal with this by itself) or use proper function to escape data, like mysqli_real_escape_string()

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141