-2

I'm php begginer and i'm trying to connect database using XAMPP. When i open my file there is not any error about connecting to database, but also there isn't "Connected successfully"; text, there is just blank page.

This is my code:

<html>
<head>
  <title></title>
</head>
<body>


    <?php
    $servername = "localhost";
    $username = "root";
    $password = "lala2";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connected successfully";
    mysqli_close($conn); 



    ?> 

    </body>
    </html>
luke1
  • 1
  • 1
  • 1

2 Answers2

2

You used a mix between mysqli and mysql ! Here is the exact method name you should use to connect to a database, and its connection status checks :

MYSQLI style :

/* database connection information */
$server = ""; 
$user = ""; 
$password = "";
$database = "";

/* error messages */
$messErr_connectionDatabaseFailed = "Error : connection failed. Please try later.";

$link = new mysqli($server, $user, $password, $database);

/* If connection failed */
if (!$link) {
    printf($messErr_connectionDatabaseFailed);
    printf("<br />");
}
/* If connection successed */
else {
    /* everything is ok, go to next part of you algorithm */
}

MYSQL style (depreciated due to performance and security issues) :

/* database connection information */
$server = ""; 
$user = ""; 
$password = "";
$database = "";

/* error messages */
$messErr_connectionDatabaseFailed = "Error : connection failed. Please try later.";

$link = mysql_connect($server, $user, $password);

/* if connection failed */
if (!$link) {
    printf($messErr_connectionDatabaseFailed);
    printf("<br />");
}
else {
    /* selecting the database */
    mysql_select_db($database, $link);

    /* guessing your select db doesn't failed, next part of you algorithm here */
}
Anwar
  • 4,162
  • 4
  • 41
  • 62
0

Please use the PDO functions to connect to the database instead. It will be easier to scale your app if you do decide to use different database drivers. Also "binding parameters" is considerably easier using PDO. For a quick intro on PDO, please read http://php.net/manual/en/intro.pdo.php

As far as why you are getting a blank page, there could be many reasons for that. You want to look at your log files first. On Windows, check the Event Logs. On Linux, your logs will be on /var/log. Add the following lines at the beginning of your script:

ini_set('display_errors',1);
error_reporting(E_ALL);

Please also look at this thread from StackOverflow: PHP produces a completely white page, no errors, logs, or headers.

Community
  • 1
  • 1
Ravi Gehlot
  • 1,099
  • 11
  • 17