Page 240 - Open Soource Technologies 304.indd
P. 240

Web Technologies-I



                   Notes         --with-mysql[=DIR] include MySQL support. DIR is the MySQL base directory. If unspecified,
                                 the bundled MySQL library will be used. --with-oci8 [=DIR] include Oracle-oci8 support. Default
                                 DIR is ORACLE_HOME. --with-ibm-db2[=DIR] include IBM DB2 support. DIR is the DB2 base
                                 install directory,  defaults  to /home/db2inst1/sqllib  --with-pgsql[=DIR]  Include  PostgreSQL
                                 support. DIR is the PostgreSQL base install directory, defaults to /usr/local/pgsql.
                                 You cannot build PHP with support for a database whose client libraries you do not have on
                                 your system. For example, if you do not have the Oracle client libraries, you cannot build PHP
                                 with support for Oracle databases. Use the phpinfo( ) function to check for database support
                                 in your installation of PHP.
                                 The MySQL database is very often used with PHP.
                                 10.1.1 Create a Connection to a MySQL Database
                                 Before you can access data in a database, you must create a connection to the database.
                                 In PHP, this is done with the mysql_connect() function.
                                 Syntax
                                 mysql_connect(servername,username,password);
                                   Parameter          Description

                                   servername         Optional. Specifies the server to connect to. Default value is
                                                      “localhost:3306”

                                   username           Optional. Specifies the username to log in with. Default value is the
                                                      name of the user that owns the server process
                                   password           Optional. Specifies the password to log in with. Default value is “”

                                 Note: There are more available parameters, but the ones listed above are the most important.



                                 In the following example we store the connection in a variable ($con) for later use in the script.
                                 The “die” part will be executed if the connection fails:


                                 <?php
                                 $con = mysql_connect(“localhost”,”peter”,”abc123”);
                                 if (!$con)
                                 {
                                 die(‘Could not connect: ‘ . mysql_error());
                                 }



                                 // some code
                                 ?>

                                 10.1.2 Closing a Connection
                                 The connection will be closed automatically when the script ends. To close the connection before,
                                 use the mysql_close() function:



                                 <?php
                                 $con = mysql_connect(“localhost”,”peter”,”abc123”);


        234                               LOVELY PROFESSIONAL UNIVERSITY
   235   236   237   238   239   240   241   242   243   244   245