function fetch_data() { global $dbh; //enumerate tables $res = pmb_mysql_list_tables(DATA_BASE); $i = 0; while ($i < pmb_mysql_num_rows($res)) { $update_a_faire = 0; /* permet de gérer les id auto_increment qui auraient pour valeur 0 */ $table_name = pmb_mysql_tablename($res, $i); bzwrite($this->fptr, "delete from {$table_name};\n"); $this->dump .= "delete from {$table_name};\n"; //parse the field info first $res2 = pmb_mysql_query("select * from {$table_name} order by 1 ", $dbh); $nf = pmb_mysql_num_fields($res2); $nr = pmb_mysql_num_rows($res2); $fields = ''; $values = ''; for ($b = 0; $b < $nf; $b++) { $fn = pmb_mysql_field_name($res2, $b); $ft = pmb_mysql_field_type($res2, $b); $fs = pmb_mysql_field_len($res2, $b); $ff = pmb_mysql_field_flags($res2, $b); $is_numeric = false; switch (strtolower($ft)) { case "int": $is_numeric = true; break; case "blob": $is_numeric = false; break; case "real": $is_numeric = true; break; case "string": $is_numeric = false; break; case "unknown": switch (intval($fs)) { case 4: // little weakness here... // there is no way (thru the PHP/MySQL interface) // to tell the difference between a tinyint and a year field type $is_numeric = true; break; default: $is_numeric = true; break; } break; case "timestamp": $is_numeric = true; break; case "date": $is_numeric = false; break; case "datetime": $is_numeric = false; break; case "time": $is_numeric = false; break; default: //future support for field types that are not recognized //(hopefully this will work without need for future modification) $is_numeric = true; //I'm assuming new field types will follow SQL numeric syntax.. // this is where this support will breakdown break; } $fields ? $fields .= ', ' . $fn : ($fields .= $fn); $fna[$b] = $fn; $ina[$b] = $is_numeric; } //parse out the table's data and generate the SQL INSERT statements in order to replicate the data itself... for ($c = 0; $c < $nr; $c++) { $row = pmb_mysql_fetch_row($res2); $values = ''; for ($d = 0; $d < $nf; $d++) { $data = strval($row[$d]); if ($d == 0 && strval($row[$d]) == 0) { /* traiter ici l'insertion avec valeur 1 pour id autoincrement et update à suivre */ $values ? $values .= ', ' . '1' : ($values .= '1'); $cle_update = pmb_mysql_field_name($res2, 0); $update_a_faire = 1; } else { if ($ina[$d] == true) { $values ? $values .= ', ' . intval($data) : ($values .= intval($data)); } else { $values ? $values .= ", \"" . pmb_mysql_escape_string($data) . "\"" : ($values .= "\"" . pmb_mysql_escape_string($data) . "\""); } } } bzwrite($this->fptr, "insert into {$table_name} ({$fields}) values ({$values});\n"); $this->dump .= "insert into {$table_name} ({$fields}) values ({$values});\n"; if ($update_a_faire == 1) { $update_a_faire = 0; bzwrite($this->fptr, "update {$table_name} set " . $cle_update . "='0' where " . $cle_update . "='1';\n"); $this->dump .= "update {$table_name} set " . $cle_update . "='0' where " . $cle_update . "='1';\n"; } } pmb_mysql_free_result($res2); $i++; } }
function desc_table($dbh, $table) { $querry = "Select * from {$table}"; $res = pmb_mysql_query($querry, $dbh); $nbr = pmb_mysql_num_fields($res); if ($nbr) { for ($j = 0; $j < $nbr; $j++) { $desc_table[$j][0] = pmb_mysql_field_name($res, $j); $desc_table[$j][1] = pmb_mysql_field_type($res, $j); $desc_table[$j][2] = pmb_mysql_field_len($res, $j); $desc_table[$j][3] = estNumerique($desc_table[$j][1], $desc_table[$j][2]); // j'ai seulement besoin de l'auto_increment, $desc_table[$j][4] est vrai si trouv?! $desc_table[$j][4] = array_search("auto_increment", explode(" ", pmb_mysql_field_flags($res, $j))); } //print_r($desc_table); return $desc_table; } }
function table_dump($table_name, $fp) { global $dbh; fwrite($fp, "#" . $table_name . "\r\n"); fwrite($fp, "drop table if exists " . $table_name . ";\r\n"); //Get strucutre fwrite($fp, create_statement($table_name) . "\n"); //enumerate tables $update_a_faire = 0; /* permet de gérer les id auto_increment qui auraient pour valeur 0 */ //parse the field info first $res2 = pmb_mysql_query("select * from {$table_name} order by 1 ", $dbh); if ($res2) { $nf = pmb_mysql_num_fields($res2); $nr = pmb_mysql_num_rows($res2); } $fields = ''; $values = ''; if ($nf) { for ($b = 0; $b < $nf; $b++) { $fn = pmb_mysql_field_name($res2, $b); $ft = pmb_mysql_field_type($res2, $b); $fs = pmb_mysql_field_len($res2, $b); $ff = pmb_mysql_field_flags($res2, $b); $is_numeric = false; switch (strtolower($ft)) { case "int": $is_numeric = true; break; case "blob": $is_numeric = false; break; case "real": $is_numeric = true; break; case "string": $is_numeric = false; break; case "unknown": switch (intval($fs)) { case 4: // little weakness here... // there is no way (thru the PHP/MySQL interface) // to tell the difference between a tinyint and a year field type $is_numeric = true; break; default: $is_numeric = true; break; } break; case "timestamp": // Afin de résoudre le pb des timestamp pas corrects en restauration $is_numeric=true; $is_numeric = false; break; case "date": $is_numeric = false; break; case "datetime": $is_numeric = false; break; case "time": $is_numeric = false; break; case "geometry": $is_numeric = false; break; default: //future support for field types that are not recognized //(hopefully this will work without need for future modification) $is_numeric = true; //I'm assuming new field types will follow SQL numeric syntax.. // this is where this support will breakdown break; } (string) $fields != "" ? $fields .= ', ' . $fn : ($fields .= $fn); $fna[$b] = $fn; $ina[$b] = $is_numeric; } } //parse out the table's data and generate the SQL INSERT statements in order to replicate the data itself... if ($nr) { for ($c = 0; $c < $nr; $c++) { $row = pmb_mysql_fetch_row($res2); $values = ''; for ($d = 0; $d < $nf; $d++) { $data = strval($row[$d]); if ($ina[$d] == true) { (string) $values != "" ? $values .= ', ' . floatval($data) : ($values .= floatval($data)); } else { (string) $values != "" ? $values .= ", \"" . pmb_mysql_real_escape_string($data) . "\"" : ($values .= "\"" . pmb_mysql_real_escape_string($data) . "\""); } } fwrite($fp, "insert into {$table_name} ({$fields}) values ({$values});\r\n"); if ($update_a_faire == 1) { $update_a_faire = 0; fwrite($fp, "update {$table_name} set " . $cle_update . "='0' where " . $cle_update . "='1';\r\n"); } } } if ($res2) { pmb_mysql_free_result($res2); } }