Alexander Crammer
Alexander Crammer

Reputation: 13

Why is MySQLi stopping the rest of my page from parsing and stopping my PHP script?

I've been trying to do some database stuff today and I've run into a bit of an issue when I realised that MySQLi keeps breaking my page. Allow me to explain. This is what I have on my server at Exd.php

<!DOCTYPE html>
<html>
    <head>
        <title>ExD</title>
    <!-- Metadata -->
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <?php
            echo "Script begins here";
            $link = mysqli_connect('localhost', 'root', '<Password>','ExD');
            if (!$link) {
                die('Could not connect: ' . mysql_error());
            }
            echo 'Connected successfully';
            mysql_close($link);
            echo "Script ends here";
            ?>
    </body>
</html>

When I request this page from Chrome on my Mac, or even Chrome on the same machine serving this file, the only output on the page is from that first echo where I said the script began. If I look at the source of the page on the client this is what shows (all that shows):

<!DOCTYPE html>
<html>
    <head>
        <title>ExD</title>
    <!-- Metadata -->
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        Script begins here

ExD is a database which I've created in MySQL, and it contains one table which is named 'Person' with the columns of 'id' and 'name' (It's a basic table for debugging.)

Here is my PHP.ini file: ExD PHP.ini on PasteBin

I can also provide my phpinfo() if you really need it. Just a note -- I've looked at phpinfo() and all of the MySQL related cells (and MySQLi related cells) have values of the names of the people who have implemented them for PHP, so I'm guessing that means they work properly and they're enabled.

Why in the world is this happening? Why is MySQLi breaking the page?

EDIT: I've managed to make progress. Now I'm able to connect to the database and I'm given the 'Connected successfully' message on my page, but immediately after that is printed to the page it breaks again, so when I view the source in Chrome I see:

<!DOCTYPE html>
<html>
    <head>
        <title>ExD</title>
    <!-- Metadata -->
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        Script begins hereConnected successfully

So the page is still breaking but MySQLi is working until I call mysql_close($link);.

I've also made the following changes to PHP.ini:

extension_dir = "C:/PHP/ext"

extension=php_mysqli.dll (uncommented it)

You can visit my site again to see a demonstration! I'm a bit lost now as to exactly why this would happen.

EDIT: Thanks so much to rack_nilesh for noticing that I was calling the wrong extension methods! Everything is working as it should now. I'm so happy!

Upvotes: 0

Views: 249

Answers (3)

frantsium
frantsium

Reputation: 155

If u want to use mysql (not recommended) then change mysqli_connect to mysql_connect

Upvotes: 0

Marek
Marek

Reputation: 7423

I bet you don't have mysqli extension installed or enabled. Check http://php.net/manual/en/mysqli.installation.php

Also do what @Naruto said.

Upvotes: 0

rack_nilesh
rack_nilesh

Reputation: 553

You have written mysqli_connect.

So mysql_error and mysql_close will be mysqli_error and mysqli_close respectively.

Also confirm if password is correct and turn error reporting on.

Upvotes: 1

Related Questions