views:

287

answers:

3

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, the $link produces no errors)

Thanks!

+2  A: 

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
Why is it I always feel like a complete dumb a$$ after I read an answer... haha .. thanks for the catch!
Marty
We all are guilty of doing things like this.
MitMaro
+2  A: 

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.

middaparka
+1  A: 

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
How is doing the error check in one line in place of two any different?
MitMaro
Sorry I didn't see your error checking after the select_db line :)
Al