I have a piece of PHP code as below:
$DB_FIRST_NAME = 'NYC';
$DB_SECOND_NAME = 'SFBAY';
$DB_HOST = 'localhost';
$DB_USER = '***';
$DB_PASS = '***';
$db_connect_first = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_FIRST_NAME);
$select_query_first = "SELECT field1, field2, field3 FROM names WHERE date > '2014-04-04 00:00:00'";
$query_result_first = $db_connect_first->query($select_query_first);
$db_connect_second = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_SECOND_NAME, true);
while($row = $query_result_first->fetch_assoc()){
$field1 = $row['field1'];
$field2 = $row['field2'];
$field3 = $row['field3'];
$insert_query_second = "INSERT INTO hello (firstname, lastname, dob) VALUES ('$field1', '$field2', '$field3')";
$insert_en_result = $db_connect_second->query($insert_query_second);
}
The SELECT query returns 20 rows; however, the INSERT query only inserts twelve rows into the hello table. I get this error for the queries which are not inserted into the table mysqli_error() expects parameter 1 to be mysqli, boolean given in.... Would you please take a look and let me know the problem?
Note: Select query has no error, and the code perfectly echos the $field1, $field2 and $field3 inside the while loop. But, Insert query not working for all rows.
Also, table structures are similar but they have different field names.
CREATE TABLE IF NOT EXISTS names (
`id` int(11) NOT NULL AUTO_INCREMENT,
`field1` text NOT NULL,
`field2` text NOT NULL,
`field3` text NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
CREATE TABLE IF NOT EXISTS hello (
id int(11) NOT NULL AUTO_INCREMENT,
firstname text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
lastname text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
dob mediumtext CHARACTER SET utf8 COLLATE utf8_unicode_ci,
PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=400 ;
below is the picture from Describe hello query in database.
also, below is the result from SHOW INDEX FROM hello

die()within the while loop, the script is going to close and will not complete execution. The issue is likely due to the 4th row that you're retrieving. Please have a look at this try/catch method, instead