Wednesday, 15 February 2012

Trouble with PHP's MySQLI flag MYSQL_PRI_KEY_FLAG -



Trouble with PHP's MySQLI flag MYSQL_PRI_KEY_FLAG -

i came across unusual behaviour , want know if doing misstake or api indeed working way.

i talking mysqli, own php frameowrk can analyze table build dynamic entity class out of works far, im struggling mysqli_pri_key_flag should tell me if column's index part of primary_key index.

however given next table:

ip_ranges

cols are:

account_id, min, max, comment

the index on table unique index name:

account_id_unique

and on 3 columns:

account_id, min, max

there definetly no primary_key defined.

so far good, lastly info need array $this->columns contains columns info fetched with:

mysqli_result::fetch_fields

now utilize next 2 fellow member functions determine if column has flag primary_key:

public function columnhasflag($columnname, $flag) { $retval = false; if(isset($this->columns[strtolower($columnname)]) == true && $this->columns[strtolower($columnname)]["flags"] & $flag) { $retval = true; } homecoming $retval; } public function determineprimarykeycolumn() { if($this->columnprimarykey === null) { foreach($this->columns $columnname => $data) { if($this->columnhasflag($columnname, mysqli_pri_key_flag) == true) { $this->columnprimarykey = $columnname; break; } } } }

the unusual illustration table above, when utilize 'vardump' on $this->columns[strtolower($columnname)]["flags"] contains flags column "account_id" returns me value: "int(49155)"

and columnhasflag returns me "true" statement: $this->columns[strtolower($columnname)]["flags"] & $flag

($flag evaluates constant mysqli_pri_key_value evaluates "2".

so how possible flag set? thought flag set primary key index must have name primary? or set other unique_key aswell?

however, seems rather unusual because there seperate flags unique , primary key in mysqli_result::fetch_field documentation:

not_null_flag = 1 pri_key_flag = 2 unique_key_flag = 4 ... (http://php.net/manual/en/mysqli-result.fetch-field.php)

so question, bug or comparing flags wrong somehow? or did miss in documentation?

i don't think bug, here quote mysql reference manual

a unique index may displayed pri if cannot contain null values , there no primary key in table. unique index may display mul if several columns form composite unique index; although combination of columns unique, each column can still hold multiple occurrences of given value.

this code used test behavior.

$flags = array( 'not_null_flag' => mysqli_not_null_flag, 'pri_key_flag' => mysqli_pri_key_flag, 'unique_key_flag' => mysqli_unique_key_flag, 'multiple_key_flag' => mysqli_multiple_key_flag, 'blob_flag' => mysqli_blob_flag, 'unsigned_flag' => mysqli_unsigned_flag, 'zerofill_flag' => mysqli_zerofill_flag, 'binary_flag' => mysqli_binary_flag, 'enum_flag' => mysqli_enum_flag, 'auto_increment_flag' => mysqli_auto_increment_flag, 'timestamp_flag' => mysqli_timestamp_flag, 'set_flag' => mysqli_set_flag, 'part_key_flag' => mysqli_part_key_flag, 'num_flag' => mysqli_num_flag ); function info_array( $result ) { homecoming array_reduce( $result->fetch_fields(), function( $result, $info ) { $result[ $info->name ] = $info; homecoming $result; } ); } function get_flags( $meta, $flags ) { homecoming array_filter( $flags, function( $flag ) utilize ( $meta ) { homecoming $meta->flags & $flag; } ); } $result = $mysqli->query( 'select * ip_ranges limit 1' ); $column_flags = array(); foreach( info_array( $result ) $name => $value ) { $column_flags[ $name ] = get_flags( $value, $flags ); } print_r( $column_flags );

so created table in db unique index on 3 columns got output said, 3 columns have primary key flag set.

array ( [account_id] => array ( [not_null_flag] => 1 [pri_key_flag] => 2 [part_key_flag] => 16384 [num_flag] => 32768 ) [min] => array ( [not_null_flag] => 1 [pri_key_flag] => 2 [part_key_flag] => 16384 [num_flag] => 32768) [max] => array ( [not_null_flag] => 1 [pri_key_flag] => 2 [part_key_flag] => 16384 [num_flag] => 32768 ) [comment] => array ( [not_null_flag] => 1 ) )

a show columns query ( show columns ip_ranges ) in mysql admin got me same result.

field type null key default account_id int(11) no pri null min int(11) no pri null max int(11) no pri null comment varchar() no null

then created primary key, , got next output:

array ( [id] => array ( [not_null_flag] => 1 [pri_key_flag] => 2 [auto_increment_flag] => 512 [part_key_flag] => 16384 [num_flag] => 32768 ) [account_id] => array ( [not_null_flag] => 1 [multiple_key_flag] => 8 [part_key_flag] => 16384 [num_flag] => 32768 ) [min] => array( [not_null_flag] => 1 [part_key_flag] => 16384 [num_flag] => 32768 ) [max] => array ( [not_null_flag] => 1 [part_key_flag] => 16384 [num_flag] => 32768 ) [comment] => array ( [not_null_flag] => 1 ) ) field type null key default id int(11) no pri null auto_increment account_id int(11) no mul null min int(11) no null max int(11) no null comment varchar() no null

so in end, php , mysql reporting same thing.

php mysqli indexing

No comments:

Post a Comment