SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

PHP MySQL Connecting string

Connection to Mysql database can be established by using mysql_connect function. We can check the success of the function by checking the result. We will get a true result in case connection is established. Based on this we can even print a message saying the details. This function takes three parameters, first one is hostname then user-id and them password. We can give the port number along with the hostname also.

Here is the function to connect to mysql database





mysql_connect ("$servername","$dbuser","$dbpassword");

The above function will return true or false depending on the success of the connection. So we will add message to the above function like this.
Related Tutorial
Printing Error message
Left Join of tables
PHP MySQL data display


$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}

Mysql_connect once establish the connection the link will be present till the script execution is over. It will close it self once the script execution is over or the function mysql_close() is called.

Connecting string with database selection. mysql_select_db() function
We can create a connection string and database connections at one go. This is required where all over the script we are using one database so better to select the database just after the connection is established. Here is the code.

$servername='localhost';

// username and password to log onto db server
$dbusername='userid';
$dbpassword='password';

// name of database
$dbname='db_name';


////////////////////////////////////////
////// DONOT 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());
}


Discuss this tutorial at forum

List of SQL Tutorials


Scripts
PHP
JavaScript
SQL Tutorial List
SQL (Home)
mysql_affected_rows mysql_change_user mysql_close mysql_connect mysql_create_db mysql_data_seek mysql_db_name mysql_db_query mysql_drop_db mysql_errno mysql_error mysql_fetch_array mysql_fetch_assoc mysql_fetch_field mysql_fetch_lengths mysql_fetch_row mysql_field_flags mysql_field_len mysql_field_name mysql_field_seek mysql_field_table mysql_field_type mysql_free_result mysql_insert_id mysql_list_dbs mysql_list_fields mysql_list_tables mysql_num_fields mysql_num_rows mysql_pconnect mysql_query mysql_result mysql_select_db mysql_tablename
SQL site Map
Knowledge Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.