Successful data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Successful data

Post by mohson »

Code: Select all

/*Connecting, selecting database*/ 
$link = mysql_connect("xxxxx", "xxxx", "xxxx") 
   or die("Could not connect : " . mysql_error()); 
mysql_select_db("contact_management_system",$link) or die("Could not select database");
Can someone show me how to update this code to include a message stating that the dtaabase was successfully loaded?
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

If the script gets past this point then the database has been loaded successfully - if there were some problem, the die("message") statements would be executed and the script will stop working.

So you could simply add an echo on the next line:

Code: Select all

/*Connecting, selecting database*/ 
$link = mysql_connect("xxxxx", "xxxx", "xxxx") 
   or die("Could not connect : " . mysql_error()); 
mysql_select_db("contact_management_system",$link) or die("Could not select database"); 

/* At this point, if the script is still executing, then the database has been loaded successfully. */
echo '<BR \>Database Selected';
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I just posted this in a thread yesterday...

Code: Select all

<?php
define('DB_USER', 'user'); // USE YOUR ACTUAL LOGIN NAME
define('DB_PASSWORD', 'pass'); // USE YOUR ACTUAL PASSWORD
define('DB_HOST', 'localhost'); // USE YOUR SERVER ADDRESS
define('DB_NAME', 'mydatabase'); // VERIFY THIS IS YOUR DATABASE NAME

if (!$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)) {
    die('<h1>Could not establish a connection to the database server ' . DB_HOST . ': ' . mysql_error() . '</h1>');
} else {
    echo '<h1>We are connected!</h1>';
}

if (!mysql_select_db(DB_NAME)) {
    die('<h1>Could not select database ' . DB_NAME . ': ' . mysql_error() . '</h1>');
} else {
    echo '<h1>We are in!</h1>';
}
?>
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

I see where your coming from but I want to adpapt this:

Code: Select all

<?php
/*Connecting, selecting database*/ 
$link = mysql_connect("*****", "*****", "*****") 
   or die("Could not connect : " . mysql_error()); 
 } else {
		echo '<h1>We are connected!</h1>'; 

mysql_select_db("contact_management_system",$link) or die("Could not select database"); 

?>
Everah | Please do not post your actual login credentials in the forums. That is just down right dangerous (to you mostly).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
/* Connecting, selecting database*/
$link = mysql_connect("*****", "*****", "*****") or die("Could not connect : " . mysql_error());
if ($link) echo '<h1>We are connected!</h1>';

mysql_select_db("contact_management_system", $link) or die("Could not select database");

// This echo works because if the db selection porked, the die would have been called
echo '<h1>And the DB is good too...</h1>';

?>
Post Reply