John Finberg John Finberg - 27 days ago 6
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:

<?php
$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 {
?>

what?

<?php
};
?>

Answer

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";