Prev | Current Page 651 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

???
CHAPTER 22 ?–  SQLITE 577
?– Tip If SQLITE_ASSOC or SQLITE_BOTH is used, PHP will look to the sqlite.assoc_case configuration
directive to determine the case of the characters.
Consider an example:
$sqldb = sqlite_open("corporate.db");
$results = sqlite_query($sqldb, "SELECT * FROM employees");
while ($row = sqlite_fetch_array($results,SQLITE_BOTH)) {
echo "Name: $row[1] (Employee ID: ".$row['empid'].")
";
}
sqlite_close($sqldb);
?>
This returns the following:
Name: Jason Gilmore (Employee ID: 1)
Name: Sam Spade (Employee ID: 2)
Name: Ray Fox (Employee ID: 3)
Note that the SQLITE_BOTH option was used so that the returned columns could be
referenced both by their numerically indexed position and by their name. Although
it??™s not entirely practical, this example serves as an ideal means for demonstrating the
function??™s flexibility.
One great way to render your code a tad more readable is to use PHP??™s list() function
in conjunction with sql_fetch_array(). With it, you can both return and parse the
array into the required components all on the same line. Let??™s revise the previous
example to take this idea into account:
$sqldb = sqlite_open("corporate.db");
$results = sqlite_query($sqldb, "SELECT * FROM employees");
while (list($empid, $name) = sqlite_fetch_array($results)) {
echo "Name: $name (Employee ID: $empid)
";
}
sqlite_close($sqldb);
?>
578 CHAPTER 22 ?–  SQLITE
Consolidating sqlite_query() and sqlite_fetch_array()
The sqlite_array_query() function consolidates the capabilities of sqlite_query()
and sqlite_fetch_array() into a single function call, both executing the query and
returning the result set as an array.


Pages:
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663