Page 1 of 1

Hiding connection messages

Posted: Mon Nov 20, 2006 1:58 pm
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;
}

Posted: Mon Nov 20, 2006 3:19 pm
by feyd
The database connection messages you're echoing? Remove the lines altogether or comment them out.

Posted: Mon Nov 20, 2006 3:36 pm
by phpflixnewbie
As i said, if i do that my whole page errors.

Posted: Mon Nov 20, 2006 3:37 pm
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;
}
?>

Posted: Mon Nov 20, 2006 3:49 pm
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);

Posted: Mon Nov 20, 2006 4:04 pm
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);
?>

Posted: Mon Nov 20, 2006 4:20 pm
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.

Posted: Mon Nov 20, 2006 4:45 pm
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);
?>