如何使用 PHP PDO 检查 MySQL 中是不是存在表? [复制]
Posted
技术标签:
【中文标题】如何使用 PHP PDO 检查 MySQL 中是不是存在表? [复制]【英文标题】:How to check if a table exists in MySQL using PHP PDO? [duplicate]如何使用 PHP PDO 检查 MySQL 中是否存在表? [复制] 【发布时间】:2018-02-22 11:45:23 【问题描述】:if($count <= 0 ) // IF TABLE DOES NOT EXIST -> CREATE AND INSERT DATA
$CREATE_TABLE= "CREATE TABLE $TABLE_NAME LIKE student; INSERT $TABLE_NAME SELECT * FROM student;";
$created = $connect->exec($CREATE_TABLE);
if($created!=FALSE)
$SQL = "INSERT INTO $TABLE_NAME (name, roll_number, father_name, dob, gender, address, email, phone, department, program, semester, section) VALUES(:name, :roll_number, :father_name, :dob, :gender, :address, :email, :phone, :department, :program, :semester, :section)";
$pdo_statement = $connect->prepare($SQL);
$pdo_statement->bindparam(':name', $name);
$pdo_statement->bindparam(':roll_number', $roll_number);
$pdo_statement->bindparam(':father_name', $father_name);
$pdo_statement->bindparam(':dob', $dob);
$pdo_statement->bindparam(':gender', $gender);
$pdo_statement->bindparam(':address', $address);
$pdo_statement->bindparam(':email', $email);
$pdo_statement->bindparam(':phone', $phone);
$pdo_statement->bindparam(':department', $department);
$pdo_statement->bindparam(':program', $program);
$pdo_statement->bindparam(':semester', $semester);
$pdo_statement->bindparam(':section', $section);
$result = $pdo_statement->execute();
else if($count > 0) // IF TABLE EXIST -> INSERT DATA
$SQL = "INSERT INTO $TABLE_NAME (name, roll_number, father_name, dob, gender, address, email, phone, department, program, semester, section) VALUES (:name, :roll_number, :father_name, :dob, :gender, :address, :email, :phone, :department, :program, :semester, :section)";
$pdo_statement = $connect->prepare($SQL);
$pdo_statement->bindparam(':name', $name);
$pdo_statement->bindparam(':roll_number', $roll_number);
$pdo_statement->bindparam(':father_name', $father_name);
$pdo_statement->bindparam(':dob', $dob);
$pdo_statement->bindparam(':gender', $gender);
$pdo_statement->bindparam(':address', $address);
$pdo_statement->bindparam(':email', $email);
$pdo_statement->bindparam(':phone', $phone);
$pdo_statement->bindparam(':department', $department);
$pdo_statement->bindparam(':program', $program);
$pdo_statement->bindparam(':semester', $semester);
$pdo_statement->bindparam(':section', $section);
$result = $pdo_statement->execute();
// ELSE IF ENDS
【问题讨论】:
检查:***.com/questions/37597538/… 最好是查询 information_schema.TABLES 表。 dev.mysql.com/doc/refman/5.7/en/tables-table.html 您应该在if
语句之前发布代码,因为您似乎正在检查该表是否存在...
为什么要检查表是否存在..?
@KunalAwasthi Haan Bhai
【参考方案1】:
所以我知道您将创建表,如果它不存在并插入数据。所以先打电话
$pdo->query("CREATE TABLE $TABLE IF NOT EXISTS;");
当表存在时,它什么也不做。
然后插入你的数据。
$pdo->query("INSERT INTO $TABLE ... ");
php 中没有“if then else”!
【讨论】:
【参考方案2】:function tableExists($pdo, $table)
// Try a select statement against the table
// Run it in try/catch in case PDO is in ERRMODE_EXCEPTION.
try
$result = $pdo->query("SELECT 1 FROM $table LIMIT 1");
catch (Exception $e)
// We got an exception == table not found
return FALSE;
// Result is either boolean FALSE (no table found) or PDOStatement Object (table found)
return $result !== FALSE;
【讨论】:
以上是关于如何使用 PHP PDO 检查 MySQL 中是不是存在表? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
在 PHP 中使用 PDO 打开的 SQL 连接是不是必须关闭
在带有 PDO 的 PHP 中,如何检查最终的 SQL 参数化查询? [复制]