Page 401 - Beginning PHP 5.3
P. 401

Chapter 12: Introducing Databases and SQL
                               echo “Connection failed: “ . $e->getMessage();
                             }

                             $sql = “SELECT * FROM fruit”;

                             echo “<ul>”;

                             try {
                               $rows = $conn->query( $sql );
                               foreach ( $rows as $row ) {
                                 echo “<li>A “ . $row[“name”] . “ is “ . $row[“color”] . “</li>”;
                               }
                             } catch ( PDOException $e ) {
                               echo “Query failed: “ . $e->getMessage();
                             }

                             echo “</ul>”;
                             $conn = null;

                             ?>
                               </body>
                             </html>























                                      Figure 12-1



                         How It Works
                         After displaying an XHTML page header, the script sets up the DSN, username, and password for
                         connecting to the MySQL database:
                             $dsn = “mysql:dbname=mydatabase“;
                             $username = “root“;
                             $password = “mypass“;





                                                                                                         363





                                                                                                      9/21/09   9:11:15 AM
          c12.indd   363
          c12.indd   363                                                                              9/21/09   9:11:15 AM
   396   397   398   399   400   401   402   403   404   405   406