By Stephon Unomon
Google +
Making the connection
to MySQL...
First off, we need to be able to tell PHP to connect to a
database before we can use it. The command is straight forward -
mysql_connect().
<?php
$dbhost = "localhost"; // the db’s server
$dbname = "mysite_dbname"; // the db’s name
$dbuser = "mysite_dbuser"; // the db’s username
$dbpass = "password"; // the password for the
user
mysql_connect ($dbhost, $dbuser, $dbpass) or die
(mysql_error()); //connect to db or show error if failure
echo "Connected to database";
?>
Looking at the above code, everything is pretty self
explanatory...except the “or die” thing. This is a method of handling errors.
If for some reason PHP cannot connect to the database, the “or
die(mysql_error())” part tells PHP to show us exactly what the error was. Most of
the time it is human error - the wrong username, password, etc. was entered in
the code. Check your work!
Hope You Enjoyed This Mysql How To
Thanks for this Article..but i want to know what is the difference between mysql_connect, mysqli_connect and mysql_pconnect.
ReplyDeleteHi Nazim, thanks for checking out my blog! Im sorry it has taken a little while to get back to you. Here is basic breakdowns and I will go into more detail to make it more clear for you.
DeleteMySql_connect - Open a connection to a MySql Server.
(mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false [, int $client_flags = 0 ]]]]] )
MySql Connect - opens or reuses a connection to a Mysql server.
MySql_pconnect - Open a persistant connection to a MySQL Server.
mysql_pconnect() is just like mysql_connect() but has 2 main differences.
When you are connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.
Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use ( mysql_close() will not close links established by mysql_pconnect()).
This type of link is therefore called 'persistent'.
MySQLi_Connect - This function is an alias of: mysqli::__construct()
mysqli::__construct() ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )
Procedural style
mysqli mysqli_connect ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )
Opens a connection to the MySQL Server running on.
You can read more about these different functions here : http://us3.php.net/manual/en/book.mysql.php - which I would highly recommend you to check out, lots of great information.
I will create a post eventually covering most of these basic functions and their uses. And if you still don't understand completly just message me back here, or email me at koolphpsuite@gmail.com
ReplyDeleteThanks,
Stephon Unomon