简体   繁体   中英

PHP Runs on Terminal but Web Page is Blank

I am using PHP to connect to a locally hosted MySQL database, and displaying the results on a web page. My code goes like this:

  <div class="starter-template">
    <h1>Phone Number Leak!</h1>
    <p class="lead">P<?php echo "phone numbers leaked from Twitter"; ?></p>

<div style = "height:100vh;">
<?php

    error_reporting(-1);
    error_reporting(E_ALL);
    echo "Hello";

    $con = mysqli_connect('localhost', 'root', 'password', 'twitter_phone');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    echo "Connection established";
    $r2 = mysqli_query($con, "Select * from phone2");

    //echo "<table border = '1'>";
    while($row = mysqli_fetch_array($r2))
    {
        echo $row['author'];
        echo "<tr> <td>Checking ... </td>";
        echo "<td>" . $row['tweet'] . "</td><td>" . $row['author'] . "</td><td>" . $row['number'] . "</td>";
        echo "</tr>";
    }

    mysqli_close($con);
    echo "</table>";    
?>
</div>
<!--</table>-->
  </div>

</div><!-- /.container -->


<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="bootstrap.min.js"></script>
</body>
</html>

When I run this in the terminal as php index1.php , I see that it has fetched all the data from the tables, and it generates appropriate HTML code. But when I view it on the web page, I can't see anything. In fact, when I look at the source code, it ends after <div style = "height:100vh;"> Hello The HTML tags that follow the PHP section don't show up either. Error reporting also does nothing - all I get is a blank page with only the bits before the PHP code starts, and this last Hello message.

A simple echo command works, so it isn't a problem with the PHP setup either, right? So what's wrong?

Thanks in advance!

  1. there is no function in PHP which is mysql_connect_errno() but mysql_errno
  2. use mysql_error() to replace mysqli_connect_error()
  3. Please align to use mysql_* function or mysqli_* function, there are different
  4. Suggest using mysqli_* functions, because mysql_* functions are decrepcated.

After adding this line var_dump(function_exists('mysqli_connect')); , I found out that mysqli was not enabled.

I did have the required packages though, so turns out I only had to do sudo service apache2 restart . :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM