I need the following query made to a mysql database with the php function "PDO" allow me to send the name of the table from URL parameter and that in turn returns all the data in the table in JSON format consulted with the webservice through a browser or system that allows transforming that information to be consolidated. This is the code:
<?php
require('Toro.php');
class Hello
{
function get()
{
echo 'Hello, worlds';
}
}
class Consulta
{
function get($name=null)
{
try
{
$usuario = "bd_user";
$password = "bd_pass";
$dbh = new PDO('mysql:host=localhost;dbname=bd_name', $usuario, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (Exception $e)
{
die("Unable to connect: " . $e->getMessage());
}
try
{
if ($name != null)
{
$stmt = $dbh->prepare("SELECT * FROM" . " " . $name);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
}
else
{
$stmt = $dbh->prepare("SELECT * FROM tabla");
}
$stmt->execute();
$data = Array();
while ($result = $stmt->fetch(PDO::FETCH_ASSOC))
{
$data[] = $result;
}
echo json_encode($data);
}
catch (Exception $e)
{
echo "Failed: " . $e->getMessage();
}
}
}
Toro::serve(array(
"/country" => "Consulta",
"/country/:alpha" => "Consulta",
));
?>
As it is observed in the code, the only parameter that is sent is the ID of a record, I need that name becomes the name of the table of the database and allows to make the select * from $ table; How can I achieve this?