Nedanstående filer finns i denna katalogen. Klicka på en fil för att visa dess innehåll.
php-to-db/
common.php config.php index.php mssql1.php mssql1.phps mssql1.php~ mssql2.php mssql2.phps mysql1.php mysql2.php source.php
<?php // =========================================================================================== // // index.php // // Description: Explanation on how to connect PHP to database, valid for students // in course Databasteknik at BTH. // // Author: Mikael Roos // // ------------------------------------------------------------------------------------------- // // The content of the page // $html = <<<EOD <div> <h1>Connecting PHP to a database</h1> <p> How to connect to a database using PHP. Examples of MySQL and MS SQL Server. </p> <h2>Introduction</h2> <p> 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. </p> <h2>PHP to MySQL</h2> <p> This is an example on connecting to a MySQL from PHP using the MySQLi database connector. You can review the <a href='http://php.net/manual/en/book.mysqli.php'>manual for the MySQLi database connector here</a>. </p> <p> <a href='mysql1.php'>Example on connecting and performing a SELECT and an INSERT/UPDATE query.</a> </p> <p> <a href='mysql2.php'>Example on connecting and performing a SELECT using values from a html form.</a> </p> <h2>PHP to MS SQL Server</h2> <p> This is an example on connecting to a MS SQL Server from PHP using the Mssql database connector. You can review the <a href='http://php.net/manual/en/book.mssql.php'>manual for the Mssql database connector here</a>. </p> <p> <a href='mssql1.php'>Example on connecting and performing a SELECT and an INSERT/UPDATE query.</a> </p> <p> <a href='mssql2.php'>Example on connecting and performing a SELECT using values from a html form.</a> </p> <h2>How to do it on my own</h2> <p> 1. Download the php-files above and save them in your www-catalogue on your student account. </p> <p> 2. Point your webbrowser to 'http://www.student.bth.se/~akronym'. Change the acronym to your own and locate the php-files you downloaded. </p> <p> 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. </p> <p> 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. </p> <h2>Reference information</h2> <p> All you need to know is in the reference manual for PHP (<a href='http://php.net'>php.net</a>). Review the <a href='http://www.php.net/manual/en/getting-started.php'>Getting started</a> section. Do also review the sections on PHP <a href='http://www.php.net/manual/en/langref.php'>language reference</a> and <a href='http://www.php.net/manual/en/funcref.php'>function reference</a>. </p> EOD; // ------------------------------------------------------------------------------------------- // // Create and print out the html-page // require_once('common.php'); $title = "Connecting PHP to a database"; $charset = "iso-8859-1"; $language = "sv"; $html = <<< EOD <?xml version="1.0" encoding="{$charset}" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{$language}" lang="{$language}"> <head> <meta http-equiv="Content-Type" content="text/html; {$charset};" /> <title>{$title}</title> </head> <body> {$header} {$html} {$footer} </body> </html> EOD; header("Content-Type: text/html; charset={$charset}"); echo $html; exit; ?>
. ..: © Mikael Roos, 2009