Post by TerrorKalle on Jul 9, 2006 14:59:15 GMT -5
Hello all, this a very simple tutorial about making a connection to a MySQL Database Service in PHP
First startout with some code:
[php]
<?php
$connection = mysql_connect("localhost", "username", "Password");
?>[/php]
Explaning:
LOCALHOST
Localhost is where your mysql database is hosted, normally for a local machine its "localhost" but for eg. external databases it could be 192.168.0.171.
USERNAME
Mysql username
PASSWORD
Mysql password
Now you made the connection and are logged on to your accont. Now we need to select a database where we got our data tables so we can handle our data input, download and general manage it... To do that we are gonna need the php function called "mysql_select_db".
[php]
<?php
mysql_select_db("database name", $connection);
?>[/php]
Explaning:
DATABASE NAME
Mysql database name
To get all working, then simply use this code:
[php]
<?php
$connection = mysql_connect("localhost", "username", "password");
mysql_select_db("database name", $connection);
?>[/php]
Now you are able to connect a mysql databaae in PHP
Using MySQLi
If you are using MySQLi, then simply just add a i after the mysql* functions
Using connections with no $connection variables
[PHP]
<?php
mysql_connect("localhost", "username", "password");
mysql_select_db("database name");
?>[/PHP]
Note to users using a $connection variable on queries, put the $connection as the second parament, like the mysql_select_db function, no need to other functions like mysql_fetch_*, mysql_num_rows ect.
Debug connection
If you want to be sure there is no errors while connecting and selecting the database, this can be done with eg, mysql_error and mysql_errno.
[PHP]
<?php
mysql_connect("localhost", "kalle", "clantem2002") or die("MySQL Returned Error: " . mysql_error());
?>[/PHP]
Or in a if-constructor:
[PHP]
<?php
if($connection = !@mysql_connect("localhost", "kalle", "clantem2002"))
{
echo("Boot error, contact webmaster!");
}
?>[/PHP]
I hope you enjoyed my tutorial
First startout with some code:
[php]
<?php
$connection = mysql_connect("localhost", "username", "Password");
?>[/php]
Explaning:
LOCALHOST
Localhost is where your mysql database is hosted, normally for a local machine its "localhost" but for eg. external databases it could be 192.168.0.171.
USERNAME
Mysql username
PASSWORD
Mysql password
Now you made the connection and are logged on to your accont. Now we need to select a database where we got our data tables so we can handle our data input, download and general manage it... To do that we are gonna need the php function called "mysql_select_db".
[php]
<?php
mysql_select_db("database name", $connection);
?>[/php]
Explaning:
DATABASE NAME
Mysql database name
To get all working, then simply use this code:
[php]
<?php
$connection = mysql_connect("localhost", "username", "password");
mysql_select_db("database name", $connection);
?>[/php]
Now you are able to connect a mysql databaae in PHP
Using MySQLi
If you are using MySQLi, then simply just add a i after the mysql* functions
Using connections with no $connection variables
[PHP]
<?php
mysql_connect("localhost", "username", "password");
mysql_select_db("database name");
?>[/PHP]
Note to users using a $connection variable on queries, put the $connection as the second parament, like the mysql_select_db function, no need to other functions like mysql_fetch_*, mysql_num_rows ect.
Debug connection
If you want to be sure there is no errors while connecting and selecting the database, this can be done with eg, mysql_error and mysql_errno.
[PHP]
<?php
mysql_connect("localhost", "kalle", "clantem2002") or die("MySQL Returned Error: " . mysql_error());
?>[/PHP]
Or in a if-constructor:
[PHP]
<?php
if($connection = !@mysql_connect("localhost", "kalle", "clantem2002"))
{
echo("Boot error, contact webmaster!");
}
?>[/PHP]
I hope you enjoyed my tutorial