CHAPTER 23 INTRODUCING (Make web site) PDO The execute() method
Monday, February 11th, 2008CHAPTER 23 INTRODUCING PDO The execute() method is responsible for executing a prepared query. To do so, it requires the input parameters that should be substituted with each iterative execution. This is accomplished in one of two ways: either pass the values into the method as an array, or bind the values to their respective variable name or positional offset in the query using the bindParam() method. The first option is covered next, and the second option is covered in the upcoming introduction to bindParam(). The following example shows how a statement is prepared and repeatedly executed by execute(), each time with different parameters: // Connect to the database server $dbh = new PDO(”pgsql:host=localhost;dbname=corporate”, “websiteuser”, “secret”); // Create and prepare the query $query = “INSERT INTO product SET sku = :sku, name = :name”; $stmt = $dbh->prepare($query); // Execute the query $stmt->execute(array(’:sku’ => ‘MN873213′, ‘:name’ => ‘Minty Mouthwash’)); // Execute again $stmt->execute(array(’:sku’ => ‘AB223234′, ‘:name’ => ‘Lovable Lipstick’)); This example is revisited below, where you ll learn how to pass along query parameters by binding them using the bindParam() method. bindParam() boolean PDOStatement::bindParam (mixed parameter, mixed &variable [, int datatype [, int length [, mixed driver_options]]]) You might have noted in the previous introduction to execute() that the input_parameters parameter was optional. This is convenient because if you need to pass along numerous variables, providing an array in this manner can quickly become unwieldy. So what s the alternative? The bindParam()method offers a somewhat cleaner method for binding parameters to corresponding query placeholders. When using named parameters, parameter is the name of the column value placeholder specified in the prepared statement using the syntax :name. When using question mark parameters, parameter is the index offset of the column value placeholder as located in the query. The variable parameter stores the value to be assigned to the placeholder. It s depicted as passed by reference, because when using this method in conjunction with a prepared stored procedure, the value could be changed according to some action in the stored procedure. This feature won t be demonstrated in this section; however, after you read Chapter 32, the process should be fairly obvious. The datatype parameter explicitly sets the parameter datatype, and can be any of the following values: PDO_PARAM_NULL: SQL NULL datatype PDO_PARAM_INT: SQL INTEGER datatype PDO_PARAM_STR: SQL CHAR, VARCHAR, and other string datatypes
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.