【问题标题】:MySQL/PHP drop table not workingMySQL/PHP 删除表不起作用
【发布时间】:2013-01-28 22:00:21
【问题描述】:

我有一个函数可以很好地参数化 MySQL 查询,但我需要它来执行 DROP TABLE IF EXISTS 查询,但我没有任何运气。我没有收到任何错误,但表没有被删除,我不知道为什么。

db_query('DROP TABLE IF EXISTS temp_table', false, false);

这里是完整的功能,以防万一,但正如我所提到的,它非常适合插入、更新、删除和选择。

// $query = SQL query, $params = array of parameters (i, s, d, b), $rs = whether or not a resultset is expected, $newid = whether or not to retrieve the new ID value;
// $onedimensionkey = key required to convert array into simple one dimensional array; $admin = if true, use the admin credentials (used only for logins)
function db_query($query, $params, $rs = true, $newid = false, $onedimensionkey = false, $admin = false) {
  (!$admin) ? $link = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME) : $link = mysqli_connect(AUTH_SERVER, AUTH_USER, AUTH_PASS, AUTH_NAME);
  if (!$link) { 
    print 'Error connecting to MySQL Server. Errorcode: ' . mysqli_connect_error(); 
    exit; 
  }

  // Prepare the query and split the parameters array into bound values
  if ($sql_stmt = mysqli_prepare($link, $query)) {
    if ($params) {
      $types = '';
      $new_params = array();
      $params_ref = array();
      // Split the params array into types string and parameters sub-array
      foreach ($params as $param) {
        $types .= $param['type'];
        $new_params[] = $param['value'];
      }
      // Cycle the new parameters array to make it an array by reference
      foreach ($new_params as $key => $parameter) {
        $params_ref[] = &$new_params[$key];
      }
      call_user_func_array('mysqli_stmt_bind_param', array_merge(array($sql_stmt, $types), $params_ref));
    }
  }
  else {
    print 'Error: ' . mysqli_error($link);
    exit();
  }

  // Execute the query
  mysqli_stmt_execute($sql_stmt);

  // Store results
  mysqli_stmt_store_result($sql_stmt);

  // If there are results to retrive, do so
  if ($rs) {
    $results = array();
    $rows = array();
    $row = array();
    stmt_bind_assoc($sql_stmt, $results);
    while (mysqli_stmt_fetch($sql_stmt)) {
      foreach ($results as $key => $value) {
        $row[$key] = $value;
      }
      $rows[] = $row;
    }
    if ($onedimensionkey) {
      $i = 0;
      foreach ($rows as $row) {
        $simplearray[$i] = $row[$onedimensionkey];
        $i++;
      }
      return $simplearray;
    }
    else {
      return $rows;
    }
  }
  // If there are no results but we need the new ID, return it
  elseif ($newid) {
    return mysqli_insert_id($link);
  }

  // Close objects
  mysqli_stmt_close($sql_stmt);
  mysqli_close($link);
}

【问题讨论】:

  • 我可能错了,但从它的外观来看,如果您运行多个查询,您最终会一遍又一遍地打开和关闭与数据库的连接。为什么不为此创建一个简单的类?
  • 检查您的数据库用户是否有权删除表。
  • 一个类是否还需要为每个查询单独连接?事实上,我对 OOP 了解不多,所以我可能完全被误导了。
  • 废话...我可以发誓我检查了权限,但这就是问题所在。谢谢致命吉他。
  • 它可以将连接存储为实例变量 - 与过程代码相比的优势在于您可以拥有多个实例,每个实例都有自己的连接,而使用全局函数,您只能使用全局变量,并且仅限于一个。

标签: php mysql sql-drop


【解决方案1】:

这样试试

 db_query('DROP TABLE IF EXISTS `dbName`.`temp_table`') ;

【讨论】:

  • 这样我会收到拒绝访问错误,因为 www-data 帐户没有权限。有没有办法通过用户名/密码使用 mysql_query?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-30
  • 1970-01-01
  • 2017-03-10
  • 2013-10-31
  • 1970-01-01
  • 2015-09-16
  • 2018-07-17
相关资源
最近更新 更多