PHP ADODB Insert Records with place holder

From CoderLearner
Jump to: navigation, search

PHP ADODB Insert Record

Use Execute method to insert multiple records to the table. It is very straightforward to insert record by using the place holder in ADODB.

Syntax ADODB INSERT Record

The array is being used for the place holder.

$sql = "INSERT INTO user_infor(id,first_name,last_name, email) values(?,?,?,?) ";
$record1 = array(1, 'Paul', 'Mark', 'pm@aa.com');
$conn1->Execute($sql, $record1);

Example 1

<?php

include 'adodb5/adodb.inc.php';

$host = 'localhost';
$user = 'user2000';
$pass = 'password2000';
$dbname = 'w3cyberlearning';

$conn1 = &ADONewConnection('mysql');
$conn1->PConnect($host, $user, $pass, $dbname);


// the MySQL insert statement.
$sql = "INSERT INTO user_infor(id,first_name,last_name, email) values(?,?,?,?) ";

$my_data = array(
	 array(1, 'Paul', 'Mark', 'pm@aa.com'),
	 array(2, 'Jam', 'Gill', 'jg@aa.com'),
	 array(3, 'Mix', 'Alex', 'mlex@aa.com'),
	 array(4, 'King', 'Mix', 'km@aa.com')
);

// loop through the array 
for ($i = 0; $i < count($my_data); $i++) {
	$d = $conn1->Execute($sql, $my_data[$i]);
	if (!$d) {
		print 'error' . $conn1->ErrorMsg() . '<br>';
	}

	echo 'Success!';
	echo "<br/>";
	
}
?>