ODBC to mySQL
mysql|odbc
/**
* ODBC to mySQL
* 徐祖宁 (唠叨)
* czjsz_ah@stats.gov.cn
*
* 初次接触php+mysql的朋友,有很多是ASP的高手。为使尽快上手
* 特写此程序,用于将原有的数据库通过ODBC数据源转换到mysql
* 其实此类程序有很多,精华区中也有不少。
* 本程序的特点在于只需知道数据源名和mysql的库名即可。程序将
* 根据数据源中的表自动进行工作。
*
*/
set_time_limit(0);
$dsn = "tjtz"; // 数据源名
$dsn_name = ""; // 数据源用户名
$dsn_pass = ""; // 数据源口令
$sql = "tjtz"; // mysql库名,通常可用数据源名
$sql_host = ""; // mysql服务器名
$sql_name = ""; // mysql用户名
$sql_pass = ""; // mysql口令
$odbc = odbc_connect($dsn,$dsn_name,$dsn_pass);
$result = odbc_tables($odbc);
$fields = odbc_num_fields($result);
if(! function_exists("odbc_fetch_array")) {
// 检查函数odbc_fetch_array是否存在,若没有则定义
// 手册上说有这个函数,但4.1.2中肯定没有
// 注意,使用odbc_fetch_into回串的数组只能用下标访问
function odbc_fetch_array(&$result) {
$ar = array();
odbc_fetch_into($result,$ar);
return $ar;
}
}
// 获取库中的表
while($ar[] = odbc_fetch_array($result));
// 定义过滤函数
function filter($var) {
return ($var[3] == "TABLE");
}
// 筛选出用户表
$ar = array_filter($ar,"filter");
foreach($ar as $tab) {
$tables[] = $tab[2];
}
// 生成建表文档,若存在则不执行
if(! file_exists("$sql.sql")) {
// 提取各表的字段信息
$fp = fopen("$sql.sql","w");
foreach($tables as $table) {
$result = odbc_do($odbc,"select * from $table");
$str = sprintf("create table %s (",$table);
for($i=0;$i
$s = sprintf("%s%s %s"
,($i>0?", ":"")
,odbc_field_name($result,$i+1)
,odbc_field_type($result,$i+1)
);
}else if(eregi("COUNTER",odbc_field_type($result,$i+1))) {
$s = sprintf("%s%s TINYINT(%s) AUTO_INCREMENT PRIMARY KEY"
,($i>0?", ":"")
,odbc_field_name($result,$i+1)
,odbc_field_len($result,$i+1)
);
}else {
$s = sprintf("%s%s %s(%s)"
,($i>0?", ":"")
,odbc_field_name($result,$i+1)
,odbc_field_type($result,$i+1)
,odbc_field_len($result,$i+1)
);
}
$str .= $s;
}
$str .= ");\n";
fputs($fp,$str);
}
fclose($fp);
echo "已产生了建表表文件$sql.sql。
";
echo "请检查命令是否正确。若不需要重新建表,请将下面的if(1) 改为if(0)
";
echo "不要改变表名,要增加字段请放在后面!";
echo "确认后重新运行本程序!";
odbc_close($odbc);
exit;
}
if(1) { // 若不需要重新建表,请将if(1) 改为if(0)
echo "开始建表...
";
$mysql = mysql_connect();
mysql_drop_db($sql);
mysql_create_db($sql);
mysql_select_db($sql);
$fp = fopen("$sql.sql","r");
$buffer = fread($fp,filesize("$sql.sql"));
fclose($fp);
$ar = split("\n",$buffer);
foreach($ar as $query) {
if(trim($query) != "") {
// echo "$query
";
mysql_query($query,$mysql);
}
}
}
// 从建表文件提取表信息信息
$info = array();
$s = file("$sql.sql");
foreach($s as $value) {
if(eregi("create +table +([a-z0-9_-]+) *[\(](.+)[\)]",$value,$regs)) {
$table = $regs[1];
$info[$table] = array();
$ar = split(",",$regs[2]);
foreach($ar as $v) {
sscanf($v,"%s %s",$p,$p);
if(eregi("double|count|int",$p)) { // 这些类型不需要加'',还有那些?
$info[$table][] ="";
}else {
$info[$table][] ="'";
}
}
}
}
$mysql = mysql_connect($sql_host,$sql_name,$sql_pass);
mysql_select_db($sql);
echo "
正在将数据从DSN向mySQL转移...";
foreach($info as $key => $ar) {
$query = "select * from $key"; // 生成ODBC查询
$result = odbc_do($odbc,$query);
$mode = true;
while($rs = odbc_fetch_array($result)) {
$sql_query = "insert into $key values (";
for($i=0;$i
$sql_query .= "{$ar[$i]}{$rs[$i]}{$ar[$i]}";
}
$sql_query .= ")";
mysql_query($sql_query,$mysql);
}
}
odbc_close($odbc);
echo "
操作结束";
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
