2012-07-25 206 views
1

我是MySQLi的新手,但是我的逻辑看起来很合理。我试图将客户端从一个数据库复制到另一个;但它不起作用。这是DB林复制-from-:准备好的声明

+-------------------+------------------+------+-----+---------+----------------+ 
| Field    | Type    | Null | Key | Default | Extra   | 
+-------------------+------------------+------+-----+---------+----------------+ 
| id    | int(10) unsigned | NO | PRI | NULL | auto_increment | 
| client   | varchar(100)  | YES |  | NULL |    | 
| telephone   | varchar(14)  | YES |  | NULL |    | 
| physical_address | text    | YES |  | NULL |    | 
| email    | varchar(100)  | YES |  | NULL |    | 
| contact   | varchar(100)  | YES |  | NULL |    | 
+-------------------+------------------+------+-----+---------+----------------+ 

这里是我的PHP脚本:

<?php 
    set_time_limit(0); 

    date_default_timezone_set('Africa/Johannesburg'); 
    include('_cli_functions.php'); 

    echo "Starting conversion...\n"; 

    $billing_db = new mysqli('localhost', 'root', 'pass', 'billing'); 
    if ($billing_db->connect_error) { 
     die('Billing db connection error'); 
    } 
    else { 
     echo "Connected to billing db\n"; 
    } 

    $whmcs_db = new mysqli('localhost', 'root', 'pass', 'whmcs'); 
    if ($whmcs_db->connect_error) { 
     die('WHMCS db connection error'); 
    } 
    else { 
     echo "Connected to WHMCS db\n"; 
    } 

    if (!$client_result = $billing_db->query('SELECT id,SUBSTRING_INDEX(contact, " ", 1) AS firstname, 
SUBSTRING(contact FROM LOCATE(" ",contact)+1) AS lastname,client,SUBSTRING_INDEX(TRIM(contact_email), ";", 1) AS email, 
physical_address,telephone FROM clients ORDER BY id') 
    ) { 
     die("Error: " . $billing_db->error); 
    } 

    if ($client_result->num_rows == 0) { 
     echo "No clients to convert\n"; 
    } 
    elseif (!$client_insert_stmt = $whmcs_db->prepare('INSERT INTO tblclients (id,firstname,lastname,companyname,email,address1,country, 
phonenumber,password,currency,datecreated) values (?,?,?,?,?,?,"ZA",?,?,1,"' . date('Y-m-d') . '")') 
    ) { 
     printf("Prepared Statement Error: %s\n", $whmcs->error); 
    } 

    $client_insert_stmt->bind_param('isssssss', $client_data['id'], $client_data['firstname'], $client_data['lastname'], 
            $client_data['client'], $client_data['email'], $client_data['physical_address'], $client_data['telephone'], $password); 

    $count = 0; 
    while ($client_data = $client_result->fetch_assoc()) { 
// this is just for debugging 
     echo $client_data['id'], $client_data['firstname'], $client_data['lastname'], $client_data['client'], $client_data['email'], $client_data['physical_address'], $client_data['telephone'], $password; 
     $password = generatePassword(); 
     if (!$client_insert_stmt->execute()) { 
      die("\n" . $client_insert_stmt->error . "\n"); 
     } 

     $count++; 
     show_status($count, $client_result->num_rows); 
    } 
    $client_insert_stmt->close(); 
    echo $client_result->num_rows . " clients processed.\n\n"; 
?> 

然而,这里是我的输出:

Starting conversion... 
Connected to billing db 
Connected to WHMCS db 
1BillyBobCompany [email protected] 123 Whatever Avenue(000) 000-0000 
Column 'firstname' cannot be null 

正如你可以看到,它的输出绑定的信息,那么我做错了什么?

+0

完全没有答案,但请用4个空格缩进你的代码,并且使用花括号代替你的if语句并删除那些首标。 – PeeHaa 2012-07-25 16:19:09

+1

在设置之前,您正在使用'$ client_data'。 – 2012-07-25 16:19:59

回答

3

您正在使用bind_param()内部while循环,您可以在其中定义$client_data。取而代之,在循环中的echo语句下弹出该行。

+0

这就是诀窍,但我认为这一切的关键是我可以绑定一次,它会分配变量的'新'值每次执行? – SupaMonkey 2012-07-25 20:45:59

+1

@SupaMonkey你把“绑定”与“准备”混为一谈。准备好的陈述的重点是双重的:1)您的输入被视为字符串,因此更好地防止注入; 2)你*准备*一次陈述,然后*绑定*并执行尽可能多的次数。绑定是分配新值。 – Palladium 2012-07-25 20:50:48