我是MySQLi的新手,但我的逻辑似乎很合理。我试图将客户从一个数据库复制到另一个数据库;但它不起作用。这是DB Im copy -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
正如你所看到的那样,它输出了被绑定的信息,所以我做错了什么?
答案 0 :(得分:3)
您正在使用bind_param()
循环外while
定义$client_data
。在循环中的echo
语句下弹出该行。