Well you pretty much got it, its just a matter of understanding what it is you're doing. I create a function for connecting to the database and selecting the table which you can put on your page, or in a function library.
PHP Code:
function connect_to_database( $host, $user, $password, $dbname )
{
// Try to connect to database
$dbconnection = mysql_connect( $host, $user, $password );
if ( ! $dbconnection )
{
die();
}
// Try to select database
$dbselection = mysql_select_db( $dbname );
if ( ! $dbselection )
{
die();
}
return $dbconnection;
}
Above, before all the die functions feel free to redirect to an error page or whatever, its not too hard, figure you can get it yourself.
PHP Code:
$dbconnection = connect_to_database( 'localhost', 'george', 'password', 'wp_content');
$retrieve_sql = "SELECT * FROM wp_content";
$dbretrieve_result = mysql_query( $retrieve_sql );
if ( ! $dbretrieve_result )
{
die();
}
if ( mysql_num_rows($dbretrieve_result) != 0 )
{
while ( $row = mysql_fetch_assoc($dbretrieve_result) )
{
echo "<p>{$row['intro']}</p>";
}
}
mysql_free_result( $dbretrieve_result );
mysql_close( $dbconnection );
As I said pretty much what you had, just utilising my function, and also a few error preventatives.
PHP Code:
if ( ! $dbretrieve_result )
{
die();
}
If for some reason it can't connect, it exits the operation.
PHP Code:
if ( mysql_num_rows($dbretrieve_result) != 0 )
If the table exists, but has no actual information in it, i.e an empty set. This prevents it displaying nothing, and being happy about it : )
PHP Code:
mysql_free_result( $dbretrieve_result );
mysql_close( $dbconnection );
Just as a matter of hygiene, its nice to clean up. The first command just clears the variable of any value, and the last one closes the connection succesfully.
If theres any specifics feel free to ask. : )
Bookmarks