0

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)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Marty
  • 2,606
  • 7
  • 29
  • 35

3 Answers3

2

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.

MitMaro
  • 5,607
  • 6
  • 28
  • 52
2

Incidentally, I suspect the problem is that PHP is throwing an error, but you've disabled the display of errors - hence the display of a blank white page. Check the status of 'display_errors' within your php.ini file.

NB: If this is a production server, you should leave display_errors set to off.

John Parker
  • 54,048
  • 11
  • 129
  • 129
1

Check it really is that line by replacing this:

$db_selected = mysql_select_db('best_of', $link);

With this:

if (! $db_selected = mysql_select_db('best_of', $link)) die('Unable to select database');

As MitMaro says you've muddled _result and _query. This might be better:

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

Hope that helps :)

Al.
  • 2,872
  • 2
  • 22
  • 34