2012-07-25 2 views
1

Im는 MySQLi에 새로 추가되었지만 아래 논리는 정상적으로 보입니다. 한 데이터베이스에서 다른 데이터베이스로 클라이언트를 복사하려고합니다. 하지만 작동하지 않습니다. 여기 준비된 진술

+-------------------+------------------+------+-----+---------+----------------+ 
| 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 스크립트입니다 : 이것은 DB 임 복사 -from-입니다 그러나

<?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'문에 중괄호를 사용하고 그 oneliner를 삭제하십시오. – PeeHaa

+1

설정하기 전에'$ client_data'를 사용하고 있습니다. –

답변

3

당신은 을 정의하는 누설 문자 while 루프를 사용하고 있습니다. 루프에서 echo 문 아래에 그 라인을 팝하십시오.

+0

그게 그 트릭을 마쳤지 만,이 모든 것의 요점은 내가 한 번 바인딩 할 수 있고 실행될 때마다 변수의 '새로운'값을 할당한다는 것입니다. – SupaMonkey

+1

@SupaMonkey "바인딩"과 "준비 중"을 혼동하고 있습니다. 준비된 문장의 요점은 두 가지입니다. 1) 입력이 리터럴 문자열로 간주되므로 주입에 대한 보호가 향상됩니다. 2) 명령문을 한 번 준비한 다음 * bind *하고 원하는만큼 실행할 수 있습니다. 바인딩은 새로운 값의 할당입니다. – Palladium