将OICQ数据转成MYSQL数据_php基础

This file is /tmp/phpBeoJQ5 .  
################# You can copy this code to test #################  

<?  
include("include/dbclass.inc"); //可以用 MYSQL.INC 类代替测试  
$q = new DB_Sql;  

$db = $q->Database; //define the database  
mysql_connect('localhost:3306',$q->User,$q->Password);  
?>  

<form method="post" action="<?php echo $PHP_SELF;?>"  
enctype="multipart/form-data">  
<input type="file" name="csv_file"  
value="<?php echo $csv_file;?>">    
terminated by <input type="text" name="terminated"  
value="," size=2><p>  
Which table you want to insert ?  
<?php show_table($db,$table); ?><p>  
<input type="submit" name="submit" value="go">  
</form>  

<?php  

function show_field($db,$table,$num,$selected_field) {  
$result = mysql_db_query($db, "SHOW KEYS FROM $table") or mysql_die();  
$primary = "";  
while ($row = mysql_fetch_array($result))  
if ($row["Key_name"] == "PRIMARY")  
$primary .= "$row[Column_name], ";  
$result = mysql_db_query($db, "SHOW FIELDS FROM $table");  
if (!$result) {  
mysql_die();  
}  
else {  
echo "<select name=field$num>n";  
while ($row= mysql_fetch_array($result)) {  
$selected = ($selected_field == $row["Field"]) ? "selected" : "";  
echo "<option value=" . $row["Field"] . " $selected>" .  
$row["Field"] . "</option>n";  
}  
echo "</select>nn";  
}  
}  

function show_table($db,$selected_table) {  
$tables = mysql_list_tables($db);  
$num_tables = @mysql_numrows($tables);  
if ($num_tables == 0) {  
echo "no table yet";  
}  
else {  
$i = 0;  
echo "<select name=table>n";  
while ($i < $num_tables) {  
$table = mysql_tablename($tables, $i);  
$selected = ($selected_table == $table) ? "selected" : "";  
echo "<option value=$table $selected>$table</option>n";  
$i++;  
}  
echo "</select>nn";  
}  
}  

if ($submit) {  
if(!empty($csv_file) && $csv_file != "none") {  
//$csv_file = stripslashes($csv_file);  
$fp = fopen($csv_file, "r");  

// show first line of the csv file to select dababase table and its fields  
echo "<form method=post action=$PHP_SELF>";  

$csv_data = fgetcsv($fp, 2000, "$terminated");  
$num = count($csv_data);  
for ( $c=0; $c<$num; $c++ ) {  

$field_name[$c] = $csv_data[$c];  
echo $field_name[$c] . "  n";  

$table_field_name = "field$c";  
show_field($db,$table,$c,$$table_field_name);  
echo "<br>";  
}  

// copy the tmp file for next use  
$dest = "/tmp/csv_file.csv";  
copy($csv_file,$dest);  

echo "<input type=hidden name=csv_file value=$dest>n" .  
"<input type=hidden name=table value=$table>n" .  
"<input type=hidden name=terminated value=$terminated>n" .  
"<input type=submit name=submit value=ok></form><p>n";  

// when click 'ok' button ,start insert data  
if ($submit == "ok") {  
$NO = 1;  
while ($csv_data = fgetcsv($fp, 2000, "$terminated")) {  
//$num = count($csv_data);  
for ( $c=0; $c<$num; $c++ ) {  

$table_field_name = "field$c";  
$first_3 = substr($$table_field_name, 0, 3);  
$first_2 = substr($$table_field_name, 0, 2);  
$val_tmp = ($first_3 == "fid" || $first_3 == "int"  
|| $first_2 == "id") ?  
$csv_data[$c] : "'" . $csv_data[$c] . "'";  
$val .= $val_tmp . ",";  
$var .= $$table_field_name . ",";  
}  
$val = substr($val, 0, -1);  
$var = substr($var, 0, -1);  
$query = "INSERT INTO $table ($var) VALUES ($val)";  
$val = "";  
$var = "";  
$q->query($query);  
echo "NO. $NO $query<p>";  
$NO++;  
}  
fclose($fp);  
}  

}  
}  
?>  

时间: 2024-09-29 10:15:07

将OICQ数据转成MYSQL数据_php基础的相关文章

将OICQ数据转成MYSQL数据

This file is /tmp/phpBeoJQ5 .  ################# You can copy this code to test #################   <?  include("include/dbclass.inc"); //可以用 MYSQL.INC 类代替测试  $q = new DB_Sql;   $db = $q->Database; //define the database  mysql_connect('loc

详解PHP显示MySQL数据的三种方法_php基础

  昨天的程序是这样的: <?php  $link=mysql_connect("localhost","root","之前的管理员密码");  if(!$link) echo "没有连接成功!";  else echo "连接成功!";  mysql_select_db("infosystem", $link);  $q = "SELECT * FROM info&qu

php访问查询mysql数据的三种方法_php基础

1. $row = mysql_fetch_row($result); 返回一个规则的数组$row,$row[0]是第一个元素,$row[1]是第二个元素,依次类推... mysql_num_fields($result) 返回结果的元素个数. 2. $row = mysql_fetch_array($result); 返回一个数组$row. 举例如下: 表结构如下: username | password ------------------------------------- bourbo

几种显示数据的方法的比较_php基础

用于显示数据库数据的时候,一般用循环体来做,常用的方法有while()和for()两种语句,下面就来讲一下他们在不同情况下各自的用法. 我们来分别介绍: while()语句是可以显示全部数据,在不知道循环次数的情况下,尤其显得方便,而for()语句呢,可以输出显示从指定位置开始到指定位置结束的数据,在输出显示一定范围的数据时就用得上了.下面看一下编程实例: 我们先建一个数据库来备用:数据库名为:mydb 表名为:tbl. 用下面的语句:create table tal (idx int(3),u

用PHP实现ODBC数据分页显示一例_php基础

$pagesize = 2; //一页显示记录数 $con = odbc_connect("access_test","","",SQL_CUR_USE_ODBC) or die("无法连接ODBC数据源access_test"); //连接一个ODBC数据源$sql = "select count(*) as total from test"; //取得记录总数SQL语句$rst = odbc_exec(

把从SQL中取出的数据转化成XMl格式_php基础

使用了php的PEAR和DB<?php// +----------------------------------------------------------------------+// | PHP version 4.0                                                      |// +----------------------------------------------------------------------+// | C

新手配置 PHP 调试环境(IIS+PHP+MYSQL)_php基础

目的:配置运行PHP的环境(IIS+PHP+MYSQL+CF+Perl)支持php+cgi+asp+jsp等 操作系统:windows2000 advance server(sp3)简体中文版 需要相关软件: 一:php-4.3.0-Win32.zip ,到相应站点下载,我上传文件不方便,以下同. 二:ActivePerl-5.6.1.635-MSWin32-x86.msi ,注意,必须下载安装文件,就是说.msi的,不要下原代码了,我也不会弄原代码,加装这个支持cgi,如果你不想支持cgi也可

聊天室php&amp;amp;mysql(二)_php基础

//第二页面 name="frame2.php" <? include "../signup/mysql.php"; header("Expires: Sun, 28 Dec 1997 09:32:45 GMT"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache"); header("

聊天室php&amp;amp;mysql(一)_php基础

//第一个页面  name="index1.php" <? include "../signup/mysql.php"; $time1=time(); //$userid=983530409; echo $n; if($n==1){ $userid=time(); $nick="g".$userid; $last_time=$userid; $sql="insert into chat_user (userid,id,last_t