Archive for the ‘CodeIgniter’ Category

Verify if a table is locked by MySql

Many times you need to understand if a table still locked by mysql.
In this case you can use the following function to understand if it is or not.

* This function will check on the db name pass by the first param
* if one, or more tables, are locked by others processes
* Return true if just one of tables is locked
* @param string $db_name
* @param array $array_tables_names
* @return boolean
function tablesLocked($db_name, $array_tables_names)

//Take the instance of codeigniter
$ci = get_instance();

//Load the default configuration 
//(if your configuration name is different just change it)

//Check for locked tables
$sql = "SHOW OPEN TABLES FROM `".$db_name."`
WHERE In_use > 0
`Table` IN (";

$tables = count($array_tables_names);
for($i = 0; $i < $tables; $i++):
$sql .= "'".$array_tables_names[$i]."'";
if($i < ($tables-1)) $sql .= ",";

$sql .= ")";
$rs = $ci->db->query($sql);
return $rs->num_rows() > 0;

Categories: CodeIgniter, Functions, PHP
%d bloggers like this: