准备好的声明


Prepared statement

我是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脚本:

<?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 inc.a@b.co.za 123 Whatever Avenue(000) 000-0000
Column 'firstname' cannot be null

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

在定义$client_datawhile循环之外使用bind_param()。在循环中的echo语句下弹出该行。