-4

How can I use a variable inside a mysql select from statment? like so:

$db = $_POST['db'];
$query = "SELECT * FROM $db..";
durian
  • 510
  • 1
  • 7
  • 24

1 Answers1

2

To literally answer the question:

$db = $_POST['db'];
$query = "SELECT * FROM {$db}..";

OR

$query = "SELECT * FROM {$_POST['db']}..";

OR

$query = "SELECT * FROM ".$_POST['db']."..";

As others have said, accepting unsanitized input from the POST is a very bad idea indeed

at the very least you should do the following

$db = $mysqli->real_escape_string($_POST['db']);

which will atleast ensure that other commands will not be inserted such as INSERTS, UPDATES, or GRANT

Jesse Adam
  • 415
  • 3
  • 14