Hem | Visa källkod | XHTML | CSS | How to become a hacker

Connecting PHP to a database

How to connect to a database using PHP. Examples of MySQL and MS SQL Server.

Introduction

This instruction is valid for students with students-accounts at BTH. It provides some basic instructions on how to use PHP and HTML from your students account and it shows how you can connect to the database servers available at BTH.

PHP to MySQL

This is an example on connecting to a MySQL from PHP using the MySQLi database connector. You can review the manual for the MySQLi database connector here.

Example on connecting and performing a SELECT and an INSERT/UPDATE query.

Example on connecting and performing a SELECT using values from a html form.

PHP to MS SQL Server

This is an example on connecting to a MS SQL Server from PHP using the Mssql database connector. You can review the manual for the Mssql database connector here.

Example on connecting and performing a SELECT and an INSERT/UPDATE query.

Example on connecting and performing a SELECT using values from a html form.

How to do it on my own

1. Download the php-files above and save them in your www-catalogue on your student account.

2. Point your webbrowser to 'http://www.student.bth.se/~akronym'. Change the acronym to your own and locate the php-files you downloaded.

3. This is a unix-system. If you encounter problem, ensure that the files are worldreadable (chmod 644) and the directories can be read (chmod 755). Login to ssh.student.bth.se and review file permissions using 'ls' and perform 'chmod' if needed.

4. Use a texteditor to modify the content of the files and execute them again. Modify the files to access your own database. Its recommended to use a editor that can do SFTP.

Reference information

All you need to know is in the reference manual for PHP (php.net). Review the Getting started section. Do also review the sections on PHP language reference and function reference.

 .                        
..:    © Mikael Roos, 2009