|
|
|
PHP MySQL connecting script and function |
PHP and MySQL are part of famous LAMP ( Linux , Apache, MySQL, PHP)
combination and is most popular database in use for the web applications. PHP
and MySQL gets integrated easily in any platformes. Scripts developed in PHP
using Window platform works perfectly in when used in Linux environment. Many
PHP MySQL developers work in Windows platform and run the script in Linux web
server.
PHP is the server side scripting language and to know more about it and how PHP
work you can vist our PHP introduction page. To get the full detail of PHP
components working in the server you can use PHP command phpinfo. PHP
works equally well with MSSQL, Oracle and other databases available. We will
discuss more on MySQL here.
MySQL can be downloaded from www.mysql.org.
MySQL database is very powerful and can be used in real word demanding
projects. Different version of MySQL is available for different platforms.
MySQL once installed runs as a server in the local machine or in any remote
server. User can connect to the local or remote MySQL server by using the server
name or IP address based on the privelage they have. Once connected users can
manupulate the MySQL database by using different PHP commands. You will learn
many such PHP commands here. Now let us start with PHP commands to
connect to MySQL server.
$link=mysql_connect
("$servername","$dbuser","$dbpassword");
Here the variables $servername is equal to the name of the MySQL server,
$dbuser stores the user id and $dbpassword is the password assign to the
user id to access the mysql_server. If successful then it will return TRUE
and we can check the variable $link to check the status of the command.
if(!$link){die("Could not connect to MySQL");}
The above command will display the error message if connection is not
established. Now once the MySQL connection established then we can select
the database by using the command mysql_select_db. Here is the command.
mysql_select_db("$dbname",$link) or die ("could not open
db".mysql_error());
The above command will connect to database name stored in the variable
$dbname. In case of any error the above line will print out it. Here is the
total script below to connect from PHP to MySQL database using a function. At
the top of the code we are assigning all values requried to connect to MySQL
server.
<?
// hostname or ip of server
$servername='localhost';
// username and password to log onto db server
$dbusername='';
$dbpassword='';
// name of database
$dbname='mydb_name';
////////////// Do not edit below/////////
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect
("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open
db".mysql_error());
}
?>
Read how to check mysql support in php by using phpinfo
ASP MsSQL connection string
| |
|
|
|