Skip to content
Advertisement

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
};
?>

Advertisement

Answer

The statement needed a tilde around the variable. Here is the final statement: $query101 = "select 1 from `$id` LIMIT 1";

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement