Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Programming. Topics Database Handling (MySQL, MSSQL, ODBC)

Similar presentations


Presentation on theme: "PHP Programming. Topics Database Handling (MySQL, MSSQL, ODBC)"— Presentation transcript:

1 PHP Programming

2 Topics Database Handling (MySQL, MSSQL, ODBC)

3 Database Handling PHP can connect to – MySQL – MSSQL – Access and other databases like oracle, postgre sql etc There are separate methods available for connecting to the databases

4 MySQL and MSSQL Connection mysql_connect(dbserver,userid,password) mssql_connect(dbserver,userid,password) – These methods are used for connecting to MySQL and MSSQL server using a userid and password $con = mysql_connect(‘localhost’,’root’,’root’); – This gets a connection to the local mysql server using the credentials root and root – If server cannot be connected, it will throw an error stating the problem Note: Mysql and MSSQL can be accessed in identical way except for the preceeding mysql or mssql. So will use mysql henceforth.

5 MySQL Select DB mysql_select_db(connection,dbname) mysql_select_db($con,”test”); – This will select the db test under the server localhost – If unable to select the database, an error will be thrown

6 MySQL Execute Query mysql_query(connection,sql statement); This will execute the sql statement on the database and store the result in a variable Eg – $rs = mysql_query($con,select stmt); – The rows of select statement will be stored in $rs – $row = mysql_fetch_array($rs); This will fetch a row and store in $row Values can be accessed like - $row[“ID”] – returns value of column ID in the fetched row.

7 MySQL and MSSQL Close To close a db connection we have close method – mysql_close(connection); – mssql_close(connection); Example – mysql_close($con); – mssql_close($con);

8 ODBC Data handling Connect to a data source – odbc_connect(dsn,uname,pwd); – DSN – Data Source Name – Go to Control Panel -> Administrative Tools -> Datasources (ODBC) – Click on ODBC and select System DSN tab. – Click Add and choose Access Database (mdb) and click Finish – In the dialog that appears, give DSN Name and Description

9 ODBC Data handling Contd.. – Click on Select under Database section – Choose a mdb (access database file) and give OK $conn=odbc_connect('nwind','',''); – Here nwind is the DSN that we have created in our system to access a database – We have not given any user name or password during DSN creation – So those fields are left empty.

10 Selecting rows from a table $rs = odbc_exec($conn,$sql); – $conn – connection string – $sql – SQL query to select rows from table – $rs is the result set of the query execution odbc_fetch_row($rs) – This command fetches row by row from the result set $cid = odbc_result($rs,"CustomerID"); – This command fetches value for the column CustomerID from the current row and stores it in the variable $cid.

11 Closing Connection odbc_close(connection) – This function closes the connection obtained and releases the same to the connection pool odbc_close($conn); – The connection defined by $conn is released and no query can be executed using this connection variable hence forth.


Download ppt "PHP Programming. Topics Database Handling (MySQL, MSSQL, ODBC)"

Similar presentations


Ads by Google