PHP and Databases #1
Best Forumer,
I want to say beforehand that this tutorial I've written in Dutch, then I got it through google translated into English. Please understand this.
Caution! I make notes and explanations in scripts.
Well I want you to lay out how you can call a database with PHP.
The most common method is to use MySQL. Most hosting sites support this feature.
Chapter 1: Connect to the database
To connect to the database, we use the following code snippet:
[php]<?php
/* Database settings */
$db = array (
'host' => 'localhost', // Hostname
'user' => 'root', // Username
'pass' => '', // Password
'dbname' => 'test' // Database name
);
/* Connect to database */
# Make connection
if(!mysql_connect($db['host'], $db['user'], $db['pass']))
{
// Can't connect!
trigger_error('Error while connecting: '.mysql_error());
}
# Select database
elseif(!mysql_select_db($db['dbname']))
{
// Can't connect database!
trigger_error('Error while selecting database: '.mysql_error());
}
else
{
// Connected to database
// ...
// ...
}
?>[/php]
This was Chapter 1, soon I will go to the next chapter.
I want to say beforehand that this tutorial I've written in Dutch, then I got it through google translated into English. Please understand this.
Caution! I make notes and explanations in scripts.
Well I want you to lay out how you can call a database with PHP.
The most common method is to use MySQL. Most hosting sites support this feature.
Chapter 1: Connect to the database
To connect to the database, we use the following code snippet:
[php]<?php
/* Database settings */
$db = array (
'host' => 'localhost', // Hostname
'user' => 'root', // Username
'pass' => '', // Password
'dbname' => 'test' // Database name
);
/* Connect to database */
# Make connection
if(!mysql_connect($db['host'], $db['user'], $db['pass']))
{
// Can't connect!
trigger_error('Error while connecting: '.mysql_error());
}
# Select database
elseif(!mysql_select_db($db['dbname']))
{
// Can't connect database!
trigger_error('Error while selecting database: '.mysql_error());
}
else
{
// Connected to database
// ...
// ...
}
?>[/php]
This was Chapter 1, soon I will go to the next chapter.
!