I am always getting blank page when using Php and mysql connections
            Asked
            
        
        
            Active
            
        
            Viewed 44 times
        
    -2
            
            
        - 
                    4Do not paste code as images please. – Norrius Jan 21 '18 at 16:08
- 
                    1To add to Norrius' comment, instead of using images please paste your code directly into your question. Then you can select it and press Ctrl+K or click the `{}` button to indent it by four spaces, which will cause SO to treat it as code. – ChrisGPT was on strike Jan 21 '18 at 16:13
- 
                    You should add some error checking, it may not be able to connect to db. Avoid using root user, and also read [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) before going any further. – Lawrence Cherone Jan 21 '18 at 16:20
- 
                    You should get some information from logs, or you can change the settings to make PHP display the error messages on your page. https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – LCB Jan 21 '18 at 17:26
1 Answers
0
            
            
        Try this code instead:
<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT id, firstname, lastname FROM MyGuests";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
        }
    } else {
        echo "0 results";
    }
    $conn->close();
    ?>
 
    
    
        Stefan Avramovic
        
- 1,365
- 2
- 11
- 20
 
    