I am just wondering what I have done wrong. I successfully installed Netbeans, PHP, MySQL, APACHE on Ubuntu 11.04 and I have been developing applications with them. Recently, Whenever I try to establish a connection with a MySQL using the following lines of code, and run the page from Netbeans, i get a blank page...ie not even the "die" statement is executing on the browser and the entire page becomes blank. Whenever, I comment out these lines, the page runs normally.. I just wondering what crime I have committed..

$conn = mysql_connect("localhost", "user", "password")
            or die ("Unable to connect to the server due to an error: " .  mysql_error());
            
    if($conn){
        echo "Connection established with mysql server";
        $db = mysql_select_db($database_name)
            or die ("There were some problems while choosing database: ".  mysql_error());
    }

Please I need help because I have been stuck for the past two days just because of this.

Post also the surrounding code as there seems to be now obvious flaw with the snippet.

Thanks!! By the way, i earlier meant to say that even the die statement was not executing...

Below is the whole code I was talking about:

<?php

 $conn = mysql_connect("localhost", "user", "password")
            or die ("Unable to connect to the server due to an error: " .  mysql_error());
 
    if($conn){
        echo "Connection established with mysql server";
        $db = mysql_select_db($database_name)
            or die ("There were some problems while choosing database: ".  mysql_error());
    }
 

?>

<html>
    <head>
        <title>Login Form</title>
    </head>
    <body>
        Enter your details:
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
           User Name:  <input type="text" name="username"/><br/>
            Password: <input type="password" name="password"><br/>
            <input type="submit" name="submit">
        </form>
    </body>

As it stands, nothing is displayed on the browser when run, but whenever I comment out the php lines of the code, the form is displayed.... Thanks a lot.

$database_name is never initialized.
On my test machine the code runs fine - it shows error messages where it's supposed to.
The HTML is not well-formed. Close the <html> tag. Does it change anything?

Member Avatar for diafol

May be a silly question, but has this file got a php extension or is it .html? In addition, is netbeans storing your project under the default localhost directory? If you store the file anywhere else, you may find that the local server (Apache) won't touch it.

Thanks. I've closed HTML tag and the codes now look like this:

<?php

 $conn = mysql_connect("localhost", "user", "password")
            or die ("Unable to connect to the server due to an error: " .  mysql_error());

    if($conn){
        echo "Connection established with mysql server";
        $db = mysql_select_db($database_name)
            or die ("There were some problems while choosing database: ".  mysql_error());
    }


?>

<html>
    <head>
        <title>Login Form</title>
    </head>
    <body>
        Enter your details:
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
           User Name:  <input type="text" name="username"/><br/>
            Password: <input type="password" name="password"><br/>
            <input type="submit" name="submit">
        </form>
    </body>

    </html>

And it nothing changes... The code is in the default localhost directory. I observe that the browser only becomes blank whenever I add anything like mysqli_.... or mysql_... However, if try the following before the mysql_... or mysqli_... lines of code, it echoes normally.

<?php
    echo "What's wrongs?";
?>

I dont just know what could cause this...

Start debugging at the root.
- Establish a connection from the mysql command line to your database with the same connection parameters as in you PHP script. Does it work?
- Echo the result of each assignment line in PHP. What is the value of $conn after line 4?
- Is the source code of the retrieved HTML page really empty? Or is only the browser not displaying anything because of erroneous HTML?
- Try to call mysql from PHP with a wrong syntax. Does it raise an error?

- I have established connection from the command line using the same username, password and database, and a connection was established.

-When I echoed the value of $conn after line 4, nothing still comes up when run.

<?php

 $conn = mysql_connect("localhost", "user", "password")
            or die ("Unable to connect to the server due to an error: " .  mysql_error());
 echo "The current value of Conn variable is $conn";

    if($conn){
        echo "Connection established with mysql server";
        $db = mysql_select_db($database_name)
            or die ("There were some problems while choosing the database: ".  mysql_error());
           echo "The value of the db variable is : $db";

    }



?>

<html>
    <head>
        <title>Login Form</title>
    </head>
    <body>
        Enter your details:
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
           User Name:  <input type="text" name="username"/><br/>
            Password: <input type="password" name="password"><br/>
            <input type="submit" name="submit">
        </form>
    </body>

</html>

-Strange, the source code of retrieved HTML is SURPRISINGLY EMPTY(as in a blank file, no html tag or anything at all.

- When I try to call mysql from PHP with a wrong syntax, it does not raise any error.

Have a look in the server's error log.

After all the headache, the only I resolved the problem was to reinstall mysql and php. Sad..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.