我是 MySQLi 的新手,但我下面的逻辑似乎很合理。我试图将客户端从一个数据库复制到另一个;但它不工作。这是我正在复制的数据库 -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 inc.a@b.co.za 123 Whatever Avenue(000) 000-0000
Column 'firstname' cannot be null
如您所见,它输出绑定的信息,那么我做错了什么?