ThingEngineer / PHP-MySQLi-Database-Class

Wrapper for a PHP MySQL class, which utilizes MySQLi and prepared statements.
Other
3.3k stars 1.34k forks source link

New function columnExists #629

Open MazettePatacaisse opened 7 years ago

MazettePatacaisse commented 7 years ago

Hi bro, Here we are my little contribution! Not very complicated but usefull. I needed to check the existence of some columns, when updating my bd. I figured it might be useful to others. Simply copy the function into the myslidb.php file.

public function columnExists($table, $column){ $mod = (!is_array($column) ? "simple":"union"); $count = count($column); if ($count == 0) { return false; } switch($mod){ case "union": $i = 0; foreach($column as $col){ $col = "%".$col."%"; $res = $this->rawQuery("SHOW COLUMNS FROM $table LIKE '$col'"); ($res?$i++:""); } return ($i === $count?true:false); case "simple": default: $column = "%".$column."%"; $res = $this->rawQueryOne("SHOW COLUMNS FROM $table LIKE '$column'"); return ($res?true:false); } }

Usage: $res = $db->columnExists($table, $column) if($res === true) .....Code...... else ........

Note: $column can be a single value or an array.

Have a good day, and really a big thank's for this wrapper!

duhow commented 7 years ago

Careful with this, because the rawQuery does not use table prefixes. Fixing and doing PR.

duhow commented 7 years ago

Done at https://github.com/joshcam/PHP-MySQLi-Database-Class/pull/670