In the event that you need to execute a particular query multiple times with different values - for example, a series of INSERT statements - the DB class comes with two methods that can save you a huge amount of time (see, there's that lazy thing again!) and also reduce overhead. Consider the following example, which demonstrates: The prepare() function, which takes an SQL query as parameter, readies a query for execution, but does not execute it (kinda like the priest that walks down the last mile with you to the electric chair). Instead, prepare() returns a handle to the prepared query, which is stored and then passed to the execute() method, which actually executes the query (bzzzt!). Note the placeholder used in the query string passed to prepare() - this placeholder is replaced by an actual value each time execute() runs on the prepared statement. The second argument to execute() contains the values to be substituted in the query string. In the event that you have multiple placeholders within the same query string, you can pass execute() an array of values instead of a single value - as demonstrated below: Although overkill for our simple needs, you should be aware that prepare() can also read data directly from a file, rather than an array. I'll leave this to you to experiment with.
blog comments powered by Disqus |