Hiding connection messages

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
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Hiding connection messages

Post by phpflixnewbie »

Hi guys,

I'd like to 'hide' the output of the database connection messages from printing to html, ive tried @ signs/deleting the echo function but this just causes the whole page to error.
My connection code is below:

Code: Select all

$Host = "localhost"; //you can use IP address instead of localhost
$User = "my_username";
$Password = "my_password";
$Database = "my_database";

$Link_ID=mysql_pconnect($Host, $User, $Password);
     if (!$Link_ID)
     {
        echo "Failed to connect to Server=".$Host;
          return 0;
     }
     else
     {
         echo "<B>Successfully to connected to Server  </B>" .$Host;
     }


     if (!@mysql_select_db($Database,$Link_ID))
     {
         echo "<br>Cannot use database=  " .$Database;
      }
      else
     {
          echo "<br> Successfully connected to database= " .$Database;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The database connection messages you're echoing? Remove the lines altogether or comment them out.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

As i said, if i do that my whole page errors.
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
$Host = 'localhost'; //you can use IP address instead of localhost
$User = 'my_username';
$Password = 'my_password';
$Database = 'my_database';

if (!$Link_ID = mysql_pconnect($Host, $User, $Password));
    echo 'Failed to connect to Server ' . $Host;
    return 0;
}

if (!mysql_select_db($Database, $Link_ID))
{
    echo '<br>Cannot use database  ' . $Database;
}
?>
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

ok, that kinda worked, my page didnt error, but now my results table doesnt get printed to my page.
Heres the full php code:

Code: Select all

$Host = "localhost"; //you can use IP address instead of localhost
$User = "my_username";
$Password = "my_password";
$Database = "my_database";

$Link_ID=mysql_pconnect($Host, $User, $Password);
     if (!$Link_ID)
     {
        echo "Failed to connect to Server=".$Host;
          return 0;
     }
     else
     {
         echo "<B>Successfully to connected to Server  </B>" .$Host;
     }


     if (!@mysql_select_db($Database,$Link_ID))
     {
         echo "<br>Cannot use database=  " .$Database;
      }
      else
     {
          echo "<br> Successfully connected to database= " .$Database;
      }
// Performing SQL query
$query = 'select dvd_title, avg(rating), dvd_rlsdate from dvd_ratings, dvd_titles where dvd_titles.dvd_id=dvd_ratings.dvd_id group by dvd_ratings.dvd_id limit 10';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "\t<tr>\n";
   foreach ($line as $col_value) {
       echo "\t\t<td>$col_value</td>\n";
   }
   echo "\t</tr>\n";
}
echo "</table>\n";


// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($Link_ID);
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Nothing that was done in the snippet I gave you would affect the remaining code. Did you remember to use your actual password and username?

Code: Select all

<?php
// Set up database connection vars
$Host = 'localhost'; //you can use IP address instead of localhost
$User = 'my_username';
$Password = 'my_password';
$Database = 'my_database';

// Try to connect to the server, set a link identifier
if (!$Link_ID = mysql_pconnect($Host, $User, $Password));
    // We are using die here because the query will choke if you are not connected
    die('Failed to connect to Server ' . $Host);
}

// Ok, now trying to connect to the database itself
if (!mysql_select_db($Database, $Link_ID))
{
    // Using die here for the same reason as above
    die('Cannot use database  ' . $Database);
}

// Let's run a query
$query = 'select dvd_title, avg(rating), dvd_rlsdate from dvd_ratings, dvd_titles where dvd_titles.dvd_id=dvd_ratings.dvd_id group by dvd_ratings.dvd_id limit 10';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "\t<tr>\n";
   foreach ($line as $col_value) {
       echo "\t\t<td>$col_value</td>\n";
   }
   echo "\t</tr>\n";
}
echo "</table>\n";


// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($Link_ID);
?>
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Yes im using correct login details, also tried the 'die' method as below (your snippets missing an opening bracket above first die line):

Code: Select all

if (!$Link_ID = mysql_pconnect($Host, $User, $Password));
{
    // We are using die here because the query will choke if you are not connected
    die('Failed to connect to Server ' . $Host);
}

// Ok, now trying to connect to the database itself
if (!mysql_select_db($Database, $Link_ID))
{
    // Using die here for the same reason as above
    die('Cannot use database  ' . $Database);
}
But as before no results are pulled from my database.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

And you for sure have data to select? If there are no error messages being thrown then I am assuming there are no errors to report.

Code: Select all

<?php
// Set up database connection vars
$Host = '<ENTER YOUR INFORMATION HERE>';
$User = '<ENTER YOUR INFORMATION HERE>';
$Password = '<ENTER YOUR INFORMATION HERE>';
$Database = '<ENTER YOUR INFORMATION HERE>';

// Try to connect to the server, set a link identifier
if (!$Link_ID = mysql_connect($Host, $User, $Password));
{
    // We are using die here because the query will choke if you are not connected
    die('Failed to connect to Server ' . $Host);
}

// Ok, now trying to connect to the database itself
if (!mysql_select_db($Database, $Link_ID))
{
    // Using die here for the same reason as above
    die('Cannot use database  ' . $Database);
}

/**
 * If we are here then we are connected to the database server and the database
 */

// Let's run a query
$sql = 'SELECT `dvd_title`, AVG(`rating`), `dvd_rlsdate` 
        FROM `dvd_ratings` r 
            INNER JOIN `dvd_titles` t
        ON t.dvd_id = r.dvd_id 
        GROUP BY r.dvd_id 
        LIMIT 0, 10';
if (!$result = mysql_query($sql))
{
    die('Query (' . $sql . ') failed because: ' . mysql_error());
}

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>\n";

// Closing connection
mysql_close($Link_ID);
?>
Post Reply