【问题标题】: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

【问题讨论】:

标签: php mysql session pdo


【解决方案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;
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-19
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      • 2012-08-12
      相关资源
      最近更新 更多