[ create a new paste ] login | about

Link: http://codepad.org/bg69zVBr    [ raw code | fork ]

PHP, pasted on Jul 1:
<?php

	// What are we inserting into the database table?
	$FirstName = 'Steve';
	$LastName = 'Ollinger';
	$Username = 'sollinger';

	// Create a DSN for MySQL (Syntax: 'mysql:host=[server];dbname=[database]')
	$MySQLDSN = 'mysql:host=localhost;dbname=TestDatabase';

	try {
		// Create a new (MySQL) PDO object (Syntax: 'PDO([DSN], [UserName], [Password]')
		$db = new PDO($MySQLDSN, 'TestUser', 'TestPassword');
		// What is your intended query?
		$sql = 'insert into users (vcFirstName,vcLastName,vcUsername) values (:FirstName,:LastName,:Username)';
		// Prepare the query
		$query = $db->prepare($sql);
		// Execute the query
		$query->execute(array(':FirstName'=>$FirstName, ':LastName'=>$LastName, ':Username'=>$Username));

		// Store any resulting errors
		$errorInfo = $db->errorInfo();
		// If there were any errors, assign them to the error variables
		if (isset($errorInfo[2])){
			$error = $errorInfo[2];
		}

	} catch (PDOException $e) {
		// Catch any PDO exceptions
		$error = $e->getMessage();
	}

	// Did an error occur during the above operation?
	if (isset($error)) {
		// Display the error
		echo $error;
	} else {
		// Indicate that all went well
		echo 'OK';
	}

?>


Create a new paste based on this one


Comments: