我想知道如何将变量合并到PHP语句中以检查表是否存在。由于某种原因,查询不接受变量。这就是我所拥有的:
<?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
};
?>
答案 0 :(得分:1)
这些陈述需要StoredProcedureQuery query = entityManager
.createStoredProcedureQuery("post_comments")
.registerStoredProcedureParameter(1, Long.class,
ParameterMode.IN)
.registerStoredProcedureParameter(2, Class.class,
ParameterMode.REF_CURSOR)
.setParameter(1, 1L);
query.execute();
List<Object[]> postComments = query.getResultList();
$ id` LIMIT 1“;
答案 1 :(得分:0)
您可以使用mysql以两种方式实现此目的,第一种是简单SELECT FROM
类型的查询:
<?php
function mysql_table_exists(mysqli $conn, $table)
{
$res = @$conn->query('SELECT 1 FROM `' . $conn->real_escape_string($table) . '`');
if($res)
{
$res->free();
return true;
}
return false;
}
?>
您也可以使用以下语句在INFORMATION_SCHEMA
中执行检查来执行此操作:
SELECT 1 FROM information_schema.tables WHERE table_schema = '<database name>' AND table_name = '<table name>' LIMIT 1;
最后,您可以调用SHOW TABLES LIKE '<table name>';
并解析匹配结果,类似于第一个示例中的概念。
答案 2 :(得分:0)
使用双引号(“...查询...”)编辑查询
$query101 = 'select 1 from "$id" LIMIT 1';
进入
$query101 = "select 1 from '".$id."' LIMIT 1";
OR
$query101 = "select 1 from $id LIMIT 1";
答案 3 :(得分:-1)
改变这一点,希望它有用
$query101 = 'select 1 from "$id" LIMIT 1';
到这个
$query101 = 'select 1 from "'.$id.'" LIMIT 1';