Search code examples
phpmysql-select-db

php mysql_select_db returns blank page


I'm just starting out writing this code and when I add in the db select and refresh the page, instead of showing all the other html on the page or an error, it just shows up blank.

Here's what I've got-

$link = mysql_connect('vps2.foo.com:3306', 'remote_vhost30', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db('best_of', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

$sections_result = "SELECT * FROM sections";
$sections_query = mysql_query($sections_result) or die(mysql_error());
$sections_array = mysql_fetch_array($sections_result) or die(mysql_error());

This code above returns a blank page. If I comment out the row starting with $db_selected the page loads fine. Obviously it doesn't do anything with the data but no errors.

What's the problem? (And yes, I am connecting to a remote server, but the $link produces no errors)


Solution

  • The last line of code should be:

    $sections_array = mysql_fetch_array($sections_query) or die(mysql_error());
    

    You are trying to fetch rows from the variable $sections_result, which is your query string and not the result set.


    Turn on error reporting, with error_reporting(E_ALL) like mentioned in one of the other answers.