John Finberg John Finberg - 1 year ago 78
MySQL Question

Check For Existing Table Using variable

I was wondering how to incorporate a variable into a PHP statement to check if a table exists. For some reason the query does not accept the variable. Here is what I have:

$servername = "localhost";
$username = "***";
$password = "***";
$dbname = "stavacom_students";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());

$id = "1";
echo $id;
$query101 = 'select 1 from "$id" LIMIT 1';
$val = mysqli_query($conn, $query101);
if($val !== FALSE){

echo "no";

} else {



Answer Source

The statements requires to sandwich the variable because my table names were numbers. Here is the final statement: $query101 = "select 1 from$id` LIMIT 1";

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download