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!
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:
<?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);
?>
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.
<?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);
?>