In my PHP script I am trying to use variable name as the table name in a MySQL statement, but when I use the variable as the table name it give me a syntax error if no ` are used and says Incorrect Table Name '' when ` are used. The code is attached below
function toQuery($tblName){
$t = "testtitle";
$art = "testarticle";
$auth = "testauthor";
return "INSERT INTO `$tblName` VALUES ('$t', '$art', '$auth', null)";
}
mysql_connect("localhost","testuser","pass123");
mysql_query("Use `test_schema`");
if(mysql_query(toQuery("test_table"))){
echo "Query: ".toQuery("test_table")." was run.";
}else{
echo "Query: ".toQuery("test_table")." was not run. ".mysql_error();
}
When I use the variable $tblName variable it echos an identical query to if i just put test_table straight in the query returned, but the one with out the variable in query executes properly.